22 #include <linux/kernel.h>
23 #include <linux/i2c.h>
24 #include <linux/errno.h>
26 #include <linux/string.h>
29 #include <linux/slab.h>
30 #include <linux/module.h>
33 #define TSL258X_MAX_DEVICE_REGS 32
36 #define TSL258X_REG_MAX 8
39 #define TSL258X_CNTRL 0x00
40 #define TSL258X_ALS_TIME 0X01
41 #define TSL258X_INTERRUPT 0x02
42 #define TSL258X_GAIN 0x07
43 #define TSL258X_REVID 0x11
44 #define TSL258X_CHIPID 0x12
45 #define TSL258X_ALS_CHAN0LO 0x14
46 #define TSL258X_ALS_CHAN0HI 0x15
47 #define TSL258X_ALS_CHAN1LO 0x16
48 #define TSL258X_ALS_CHAN1HI 0x17
49 #define TSL258X_TMR_LO 0x18
50 #define TSL258X_TMR_HI 0x19
53 #define TSL258X_CMD_REG 0x80
54 #define TSL258X_CMD_SPL_FN 0x60
55 #define TSL258X_CMD_ALS_INT_CLR 0X01
58 #define TSL258X_CNTL_ADC_ENBL 0x02
59 #define TSL258X_CNTL_PWR_ON 0x01
62 #define TSL258X_STA_ADC_VALID 0x01
63 #define TSL258X_STA_ADC_INTR 0x10
66 #define TSL258X_LUX_CALC_OVER_FLOW 65535
104 static const u8 taos_config[8] = {
105 0x00, 0xee, 0x00, 0x03, 0x00, 0xFF, 0xFF, 0x00
117 static struct taos_lux taos_device_lux[11] = {
118 { 9830, 8520, 15729 },
119 { 12452, 10807, 23344 },
120 { 14746, 6383, 11705 },
121 { 17695, 4063, 6554 },
164 for (i = 0; i < len; i++) {
168 dev_err(&client->
dev,
"taos_i2c_read failed to write"
169 " register %x\n", reg);
193 static int taos_get_lux(
struct iio_dev *indio_dev)
213 dev_err(&chip->
client->dev,
"taos_get_lux device is not enabled\n");
220 dev_err(&chip->
client->dev,
"taos_get_lux failed to read CMD_REG\n");
225 dev_err(&chip->
client->dev,
"taos_get_lux data not valid\n");
230 for (i = 0; i < 4; i++) {
232 ret = taos_i2c_read(chip->
client, reg, &buf[i], 1);
235 " register %x\n", reg);
248 "taos_i2c_write_command failed in taos_get_lux, err = %d\n",
269 ratio = (ch1 << 15) / ch0;
271 for (p = (
struct taos_lux *) taos_device_lux;
278 ch0lux = ((ch0 * p->
ch0) +
281 ch1lux = ((ch1 * p->
ch1) +
284 lux = ch0lux - ch1lux;
288 if (ch1lux > ch0lux) {
289 dev_dbg(&chip->
client->dev,
"No Data - Return last value\n");
313 lux = (lux + 500) / 1000;
333 static int taos_als_calibrate(
struct iio_dev *indio_dev)
337 unsigned int gain_trim_val;
345 "taos_als_calibrate failed to reach the CNTRL register, ret=%d\n",
354 "taos_als_calibrate failed: device not powered on with ADC enabled\n");
362 "taos_als_calibrate failed to reach the STATUS register, ret=%d\n",
370 "taos_als_calibrate failed: STATUS - ADC not valid.\n");
373 lux_val = taos_get_lux(indio_dev);
375 dev_err(&chip->
client->dev,
"taos_als_calibrate failed to get lux\n");
381 if ((gain_trim_val < 250) || (gain_trim_val > 4000)) {
383 "taos_als_calibrate failed: trim_val of %d is out of range\n",
389 return (
int) gain_trim_val;
396 static int taos_chip_on(
struct iio_dev *indio_dev)
414 als_count = (chip->
taos_settings.als_time * 100 + 135) / 270;
419 als_time = (als_count * 27 + 5) / 10;
435 dev_err(&chip->
client->dev,
"taos_chip_on failed on CNTRL reg.\n");
447 "taos_chip_on failed on reg %d.\n", i);
460 dev_err(&chip->
client->dev,
"taos_chip_on failed on 2nd CTRL reg.\n");
468 static int taos_chip_off(
struct iio_dev *indio_dev)
486 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
495 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
502 taos_chip_off(indio_dev);
504 taos_chip_on(indio_dev);
512 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
531 return sprintf(buf,
"%s\n", gain);
537 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
558 dev_err(dev,
"Invalid Gain Index (must be 1,8,16,111)\n");
568 return sprintf(buf,
"%s\n",
"1 8 16 111");
574 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
583 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
590 if ((value < 50) || (value > 650))
601 static ssize_t taos_als_time_available_show(
struct device *dev,
605 "50 100 150 200 250 300 350 400 450 500 550 600 650");
611 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
620 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
636 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
645 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
663 ret = taos_get_lux(dev_to_iio_dev(dev));
667 return sprintf(buf,
"%d\n", ret);
673 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
680 taos_als_calibrate(indio_dev);
691 for (i = 0; i <
ARRAY_SIZE(taos_device_lux); i++) {
692 offset +=
sprintf(buf + offset,
"%d,%d,%d,",
693 taos_device_lux[i].ratio,
694 taos_device_lux[i].ch0,
695 taos_device_lux[i].ch1);
696 if (taos_device_lux[i].ratio == 0) {
704 offset +=
sprintf(buf + offset,
"\n");
711 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
724 if ((n % 3) || n < 6 || n > ((
ARRAY_SIZE(taos_device_lux) - 1) * 3)) {
725 dev_info(dev,
"LUX TABLE INPUT ERROR 1 Value[0]=%d\n", n);
728 if ((value[(n - 2)] | value[(n - 1)] | value[n]) != 0) {
729 dev_info(dev,
"LUX TABLE INPUT ERROR 2 Value[0]=%d\n", n);
734 taos_chip_off(indio_dev);
737 memset(taos_device_lux, 0,
sizeof(taos_device_lux));
738 memcpy(taos_device_lux, &value[1], (value[0] * 4));
740 taos_chip_on(indio_dev);
746 taos_power_state_show, taos_power_state_store);
749 taos_gain_show, taos_gain_store);
751 taos_gain_available_show,
NULL);
754 taos_als_time_show, taos_als_time_store);
756 taos_als_time_available_show,
NULL);
759 taos_als_trim_show, taos_als_trim_store);
762 taos_als_cal_target_show, taos_als_cal_target_store);
767 taos_luxtable_show, taos_luxtable_store);
769 static struct attribute *sysfs_attrs_ctrl[] = {
770 &dev_attr_power_state.attr,
771 &dev_attr_illuminance0_calibscale.attr,
772 &dev_attr_illuminance0_calibscale_available.attr,
773 &dev_attr_illuminance0_integration_time.attr,
774 &dev_attr_illuminance0_integration_time_available.attr,
775 &dev_attr_illuminance0_calibbias.attr,
776 &dev_attr_illuminance0_input_target.attr,
777 &dev_attr_illuminance0_input.attr,
778 &dev_attr_illuminance0_calibrate.attr,
779 &dev_attr_illuminance0_lux_table.attr,
784 .attrs = sysfs_attrs_ctrl,
788 static int taos_tsl258x_device(
unsigned char *bufp)
793 static const struct iio_info tsl2583_info = {
794 .attrs = &tsl2583_attribute_group,
810 if (!i2c_check_functionality(clientp->
adapter,
813 "taos_probe() - i2c smbus byte data "
814 "functions unsupported\n");
819 if (indio_dev ==
NULL) {
821 dev_err(&clientp->
dev,
"iio allocation failed\n");
824 chip = iio_priv(indio_dev);
826 i2c_set_clientdata(clientp, indio_dev);
836 dev_err(&clientp->
dev,
"i2c_smbus_write_bytes() to cmd "
837 "reg failed in taos_probe(), err = %d\n", ret);
842 dev_err(&clientp->
dev,
"i2c_smbus_read_byte from "
843 "reg failed in taos_probe(), err = %d\n", ret);
850 if (!taos_tsl258x_device(buf)) {
851 dev_info(&clientp->
dev,
"i2c device found but does not match "
852 "expected id in taos_probe()\n");
858 dev_err(&clientp->
dev,
"i2c_smbus_write_byte() to cmd reg "
859 "failed in taos_probe(), err = %d\n", ret);
863 indio_dev->
info = &tsl2583_info;
864 indio_dev->
dev.parent = &clientp->
dev;
869 dev_err(&clientp->
dev,
"iio registration failed\n");
877 taos_chip_on(indio_dev);
887 #ifdef CONFIG_PM_SLEEP
888 static int taos_suspend(
struct device *dev)
897 ret = taos_chip_off(indio_dev);
905 static int taos_resume(
struct device *dev)
914 ret = taos_chip_on(indio_dev);
921 #define TAOS_PM_OPS (&taos_pm_ops)
923 #define TAOS_PM_OPS NULL
948 .id_table = taos_idtable,