1 /*
2  * Device driver for monitoring ambient light intensity (lux)
3  * within the TAOS tsl258x family of devices (tsl2580, tsl2581).
4  *
5  * Copyright (c) 2011, TAOS Corporation.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA	02110-1301, USA.
20  */
21 
22 #include <linux/kernel.h>
23 #include <linux/i2c.h>
24 #include <linux/errno.h>
25 #include <linux/delay.h>
26 #include <linux/string.h>
27 #include <linux/mutex.h>
28 #include <linux/unistd.h>
29 #include <linux/slab.h>
30 #include <linux/module.h>
31 #include <linux/iio/iio.h>
32 
33 #define TSL258X_MAX_DEVICE_REGS		32
34 
35 /* Triton register offsets */
36 #define	TSL258X_REG_MAX		8
37 
38 /* Device Registers and Masks */
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
51 
52 /* tsl2583 cmd reg masks */
53 #define TSL258X_CMD_REG			0x80
54 #define TSL258X_CMD_SPL_FN		0x60
55 #define TSL258X_CMD_ALS_INT_CLR	0X01
56 
57 /* tsl2583 cntrl reg masks */
58 #define TSL258X_CNTL_ADC_ENBL	0x02
59 #define TSL258X_CNTL_PWR_ON		0x01
60 
61 /* tsl2583 status reg masks */
62 #define TSL258X_STA_ADC_VALID	0x01
63 #define TSL258X_STA_ADC_INTR	0x10
64 
65 /* Lux calculation constants */
66 #define	TSL258X_LUX_CALC_OVER_FLOW		65535
67 
68 enum {
69 	TSL258X_CHIP_UNKNOWN = 0,
70 	TSL258X_CHIP_WORKING = 1,
71 	TSL258X_CHIP_SUSPENDED = 2
72 };
73 
74 /* Per-device data */
75 struct taos_als_info {
76 	u16 als_ch0;
77 	u16 als_ch1;
78 	u16 lux;
79 };
80 
81 struct taos_settings {
82 	int als_time;
83 	int als_gain;
84 	int als_gain_trim;
85 	int als_cal_target;
86 };
87 
88 struct tsl2583_chip {
89 	struct mutex als_mutex;
90 	struct i2c_client *client;
91 	struct taos_als_info als_cur_info;
92 	struct taos_settings taos_settings;
93 	int als_time_scale;
94 	int als_saturation;
95 	int taos_chip_status;
96 	u8 taos_config[8];
97 };
98 
99 /*
100  * Initial values for device - this values can/will be changed by driver.
101  * and applications as needed.
102  * These values are dynamic.
103  */
104 static const u8 taos_config[8] = {
105 		0x00, 0xee, 0x00, 0x03, 0x00, 0xFF, 0xFF, 0x00
106 }; /*	cntrl atime intC  Athl0 Athl1 Athh0 Athh1 gain */
107 
108 struct taos_lux {
109 	unsigned int ratio;
110 	unsigned int ch0;
111 	unsigned int ch1;
112 };
113 
114 /* This structure is intentionally large to accommodate updates via sysfs. */
115 /* Sized to 11 = max 10 segments + 1 termination segment */
116 /* Assumption is one and only one type of glass used  */
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 },
122 };
123 
124 struct gainadj {
125 	s16 ch0;
126 	s16 ch1;
127 };
128 
129 /* Index = (0 - 3) Used to validate the gain selection index */
130 static const struct gainadj gainadj[] = {
131 	{ 1, 1 },
132 	{ 8, 8 },
133 	{ 16, 16 },
134 	{ 107, 115 }
135 };
136 
137 /*
138  * Provides initial operational parameter defaults.
139  * These defaults may be changed through the device's sysfs files.
140  */
taos_defaults(struct tsl2583_chip * chip)141 static void taos_defaults(struct tsl2583_chip *chip)
142 {
143 	/* Operational parameters */
144 	chip->taos_settings.als_time = 100;
145 	/* must be a multiple of 50mS */
146 	chip->taos_settings.als_gain = 0;
147 	/* this is actually an index into the gain table */
148 	/* assume clear glass as default */
149 	chip->taos_settings.als_gain_trim = 1000;
150 	/* default gain trim to account for aperture effects */
151 	chip->taos_settings.als_cal_target = 130;
152 	/* Known external ALS reading used for calibration */
153 }
154 
155 /*
156  * Read a number of bytes starting at register (reg) location.
157  * Return 0, or i2c_smbus_write_byte ERROR code.
158  */
159 static int
taos_i2c_read(struct i2c_client * client,u8 reg,u8 * val,unsigned int len)160 taos_i2c_read(struct i2c_client *client, u8 reg, u8 *val, unsigned int len)
161 {
162 	int i, ret;
163 
164 	for (i = 0; i < len; i++) {
165 		/* select register to write */
166 		ret = i2c_smbus_write_byte(client, (TSL258X_CMD_REG | reg));
167 		if (ret < 0) {
168 			dev_err(&client->dev,
169 				"taos_i2c_read failed to write register %x\n",
170 				reg);
171 			return ret;
172 		}
173 		/* read the data */
174 		*val = i2c_smbus_read_byte(client);
175 		val++;
176 		reg++;
177 	}
178 	return 0;
179 }
180 
181 /*
182  * Reads and calculates current lux value.
183  * The raw ch0 and ch1 values of the ambient light sensed in the last
184  * integration cycle are read from the device.
185  * Time scale factor array values are adjusted based on the integration time.
186  * The raw values are multiplied by a scale factor, and device gain is obtained
187  * using gain index. Limit checks are done next, then the ratio of a multiple
188  * of ch1 value, to the ch0 value, is calculated. The array taos_device_lux[]
189  * declared above is then scanned to find the first ratio value that is just
190  * above the ratio we just calculated. The ch0 and ch1 multiplier constants in
191  * the array are then used along with the time scale factor array values, to
192  * calculate the lux.
193  */
taos_get_lux(struct iio_dev * indio_dev)194 static int taos_get_lux(struct iio_dev *indio_dev)
195 {
196 	u16 ch0, ch1; /* separated ch0/ch1 data from device */
197 	u32 lux; /* raw lux calculated from device data */
198 	u64 lux64;
199 	u32 ratio;
200 	u8 buf[5];
201 	struct taos_lux *p;
202 	struct tsl2583_chip *chip = iio_priv(indio_dev);
203 	int i, ret;
204 	u32 ch0lux = 0;
205 	u32 ch1lux = 0;
206 
207 	if (mutex_trylock(&chip->als_mutex) == 0) {
208 		dev_info(&chip->client->dev, "taos_get_lux device is busy\n");
209 		return chip->als_cur_info.lux; /* busy, so return LAST VALUE */
210 	}
211 
212 	if (chip->taos_chip_status != TSL258X_CHIP_WORKING) {
213 		/* device is not enabled */
214 		dev_err(&chip->client->dev, "taos_get_lux device is not enabled\n");
215 		ret = -EBUSY;
216 		goto out_unlock;
217 	}
218 
219 	ret = taos_i2c_read(chip->client, (TSL258X_CMD_REG), &buf[0], 1);
220 	if (ret < 0) {
221 		dev_err(&chip->client->dev, "taos_get_lux failed to read CMD_REG\n");
222 		goto out_unlock;
223 	}
224 	/* is data new & valid */
225 	if (!(buf[0] & TSL258X_STA_ADC_INTR)) {
226 		dev_err(&chip->client->dev, "taos_get_lux data not valid\n");
227 		ret = chip->als_cur_info.lux; /* return LAST VALUE */
228 		goto out_unlock;
229 	}
230 
231 	for (i = 0; i < 4; i++) {
232 		int reg = TSL258X_CMD_REG | (TSL258X_ALS_CHAN0LO + i);
233 
234 		ret = taos_i2c_read(chip->client, reg, &buf[i], 1);
235 		if (ret < 0) {
236 			dev_err(&chip->client->dev,
237 				"taos_get_lux failed to read register %x\n",
238 				reg);
239 			goto out_unlock;
240 		}
241 	}
242 
243 	/* clear status, really interrupt status (interrupts are off), but
244 	 * we use the bit anyway - don't forget 0x80 - this is a command*/
245 	ret = i2c_smbus_write_byte(chip->client,
246 				   (TSL258X_CMD_REG | TSL258X_CMD_SPL_FN |
247 				    TSL258X_CMD_ALS_INT_CLR));
248 
249 	if (ret < 0) {
250 		dev_err(&chip->client->dev,
251 			"taos_i2c_write_command failed in taos_get_lux, err = %d\n",
252 			ret);
253 		goto out_unlock; /* have no data, so return failure */
254 	}
255 
256 	/* extract ALS/lux data */
257 	ch0 = le16_to_cpup((const __le16 *)&buf[0]);
258 	ch1 = le16_to_cpup((const __le16 *)&buf[2]);
259 
260 	chip->als_cur_info.als_ch0 = ch0;
261 	chip->als_cur_info.als_ch1 = ch1;
262 
263 	if ((ch0 >= chip->als_saturation) || (ch1 >= chip->als_saturation))
264 		goto return_max;
265 
266 	if (!ch0) {
267 		/* have no data, so return LAST VALUE */
268 		ret = chip->als_cur_info.lux = 0;
269 		goto out_unlock;
270 	}
271 	/* calculate ratio */
272 	ratio = (ch1 << 15) / ch0;
273 	/* convert to unscaled lux using the pointer to the table */
274 	for (p = (struct taos_lux *) taos_device_lux;
275 	     p->ratio != 0 && p->ratio < ratio; p++)
276 		;
277 
278 	if (p->ratio == 0) {
279 		lux = 0;
280 	} else {
281 		ch0lux = ((ch0 * p->ch0) +
282 			  (gainadj[chip->taos_settings.als_gain].ch0 >> 1))
283 			 / gainadj[chip->taos_settings.als_gain].ch0;
284 		ch1lux = ((ch1 * p->ch1) +
285 			  (gainadj[chip->taos_settings.als_gain].ch1 >> 1))
286 			 / gainadj[chip->taos_settings.als_gain].ch1;
287 		lux = ch0lux - ch1lux;
288 	}
289 
290 	/* note: lux is 31 bit max at this point */
291 	if (ch1lux > ch0lux) {
292 		dev_dbg(&chip->client->dev, "No Data - Return last value\n");
293 		ret = chip->als_cur_info.lux = 0;
294 		goto out_unlock;
295 	}
296 
297 	/* adjust for active time scale */
298 	if (chip->als_time_scale == 0)
299 		lux = 0;
300 	else
301 		lux = (lux + (chip->als_time_scale >> 1)) /
302 			chip->als_time_scale;
303 
304 	/* Adjust for active gain scale.
305 	 * The taos_device_lux tables above have a factor of 8192 built in,
306 	 * so we need to shift right.
307 	 * User-specified gain provides a multiplier.
308 	 * Apply user-specified gain before shifting right to retain precision.
309 	 * Use 64 bits to avoid overflow on multiplication.
310 	 * Then go back to 32 bits before division to avoid using div_u64().
311 	 */
312 	lux64 = lux;
313 	lux64 = lux64 * chip->taos_settings.als_gain_trim;
314 	lux64 >>= 13;
315 	lux = lux64;
316 	lux = (lux + 500) / 1000;
317 	if (lux > TSL258X_LUX_CALC_OVER_FLOW) { /* check for overflow */
318 return_max:
319 		lux = TSL258X_LUX_CALC_OVER_FLOW;
320 	}
321 
322 	/* Update the structure with the latest VALID lux. */
323 	chip->als_cur_info.lux = lux;
324 	ret = lux;
325 
326 out_unlock:
327 	mutex_unlock(&chip->als_mutex);
328 	return ret;
329 }
330 
331 /*
332  * Obtain single reading and calculate the als_gain_trim (later used
333  * to derive actual lux).
334  * Return updated gain_trim value.
335  */
taos_als_calibrate(struct iio_dev * indio_dev)336 static int taos_als_calibrate(struct iio_dev *indio_dev)
337 {
338 	struct tsl2583_chip *chip = iio_priv(indio_dev);
339 	u8 reg_val;
340 	unsigned int gain_trim_val;
341 	int ret;
342 	int lux_val;
343 
344 	ret = i2c_smbus_write_byte(chip->client,
345 				   (TSL258X_CMD_REG | TSL258X_CNTRL));
346 	if (ret < 0) {
347 		dev_err(&chip->client->dev,
348 			"taos_als_calibrate failed to reach the CNTRL register, ret=%d\n",
349 			ret);
350 		return ret;
351 	}
352 
353 	reg_val = i2c_smbus_read_byte(chip->client);
354 	if ((reg_val & (TSL258X_CNTL_ADC_ENBL | TSL258X_CNTL_PWR_ON))
355 			!= (TSL258X_CNTL_ADC_ENBL | TSL258X_CNTL_PWR_ON)) {
356 		dev_err(&chip->client->dev,
357 			"taos_als_calibrate failed: device not powered on with ADC enabled\n");
358 		return -1;
359 	}
360 
361 	ret = i2c_smbus_write_byte(chip->client,
362 				   (TSL258X_CMD_REG | TSL258X_CNTRL));
363 	if (ret < 0) {
364 		dev_err(&chip->client->dev,
365 			"taos_als_calibrate failed to reach the STATUS register, ret=%d\n",
366 			ret);
367 		return ret;
368 	}
369 	reg_val = i2c_smbus_read_byte(chip->client);
370 
371 	if ((reg_val & TSL258X_STA_ADC_VALID) != TSL258X_STA_ADC_VALID) {
372 		dev_err(&chip->client->dev,
373 			"taos_als_calibrate failed: STATUS - ADC not valid.\n");
374 		return -ENODATA;
375 	}
376 	lux_val = taos_get_lux(indio_dev);
377 	if (lux_val < 0) {
378 		dev_err(&chip->client->dev, "taos_als_calibrate failed to get lux\n");
379 		return lux_val;
380 	}
381 	gain_trim_val = (unsigned int) (((chip->taos_settings.als_cal_target)
382 			* chip->taos_settings.als_gain_trim) / lux_val);
383 
384 	if ((gain_trim_val < 250) || (gain_trim_val > 4000)) {
385 		dev_err(&chip->client->dev,
386 			"taos_als_calibrate failed: trim_val of %d is out of range\n",
387 			gain_trim_val);
388 		return -ENODATA;
389 	}
390 	chip->taos_settings.als_gain_trim = (int) gain_trim_val;
391 
392 	return (int) gain_trim_val;
393 }
394 
395 /*
396  * Turn the device on.
397  * Configuration must be set before calling this function.
398  */
taos_chip_on(struct iio_dev * indio_dev)399 static int taos_chip_on(struct iio_dev *indio_dev)
400 {
401 	int i;
402 	int ret;
403 	u8 *uP;
404 	u8 utmp;
405 	int als_count;
406 	int als_time;
407 	struct tsl2583_chip *chip = iio_priv(indio_dev);
408 
409 	/* and make sure we're not already on */
410 	if (chip->taos_chip_status == TSL258X_CHIP_WORKING) {
411 		/* if forcing a register update - turn off, then on */
412 		dev_info(&chip->client->dev, "device is already enabled\n");
413 		return -EINVAL;
414 	}
415 
416 	/* determine als integration register */
417 	als_count = (chip->taos_settings.als_time * 100 + 135) / 270;
418 	if (!als_count)
419 		als_count = 1; /* ensure at least one cycle */
420 
421 	/* convert back to time (encompasses overrides) */
422 	als_time = (als_count * 27 + 5) / 10;
423 	chip->taos_config[TSL258X_ALS_TIME] = 256 - als_count;
424 
425 	/* Set the gain based on taos_settings struct */
426 	chip->taos_config[TSL258X_GAIN] = chip->taos_settings.als_gain;
427 
428 	/* set chip struct re scaling and saturation */
429 	chip->als_saturation = als_count * 922; /* 90% of full scale */
430 	chip->als_time_scale = (als_time + 25) / 50;
431 
432 	/* TSL258x Specific power-on / adc enable sequence
433 	 * Power on the device 1st. */
434 	utmp = TSL258X_CNTL_PWR_ON;
435 	ret = i2c_smbus_write_byte_data(chip->client,
436 					TSL258X_CMD_REG | TSL258X_CNTRL, utmp);
437 	if (ret < 0) {
438 		dev_err(&chip->client->dev, "taos_chip_on failed on CNTRL reg.\n");
439 		return ret;
440 	}
441 
442 	/* Use the following shadow copy for our delay before enabling ADC.
443 	 * Write all the registers. */
444 	for (i = 0, uP = chip->taos_config; i < TSL258X_REG_MAX; i++) {
445 		ret = i2c_smbus_write_byte_data(chip->client,
446 						TSL258X_CMD_REG + i,
447 						*uP++);
448 		if (ret < 0) {
449 			dev_err(&chip->client->dev,
450 				"taos_chip_on failed on reg %d.\n", i);
451 			return ret;
452 		}
453 	}
454 
455 	usleep_range(3000, 3500);
456 	/* NOW enable the ADC
457 	 * initialize the desired mode of operation */
458 	utmp = TSL258X_CNTL_PWR_ON | TSL258X_CNTL_ADC_ENBL;
459 	ret = i2c_smbus_write_byte_data(chip->client,
460 					TSL258X_CMD_REG | TSL258X_CNTRL,
461 					utmp);
462 	if (ret < 0) {
463 		dev_err(&chip->client->dev, "taos_chip_on failed on 2nd CTRL reg.\n");
464 		return ret;
465 	}
466 	chip->taos_chip_status = TSL258X_CHIP_WORKING;
467 
468 	return ret;
469 }
470 
taos_chip_off(struct iio_dev * indio_dev)471 static int taos_chip_off(struct iio_dev *indio_dev)
472 {
473 	struct tsl2583_chip *chip = iio_priv(indio_dev);
474 
475 	/* turn device off */
476 	chip->taos_chip_status = TSL258X_CHIP_SUSPENDED;
477 	return i2c_smbus_write_byte_data(chip->client,
478 					TSL258X_CMD_REG | TSL258X_CNTRL,
479 					0x00);
480 }
481 
482 /* Sysfs Interface Functions */
483 
taos_power_state_show(struct device * dev,struct device_attribute * attr,char * buf)484 static ssize_t taos_power_state_show(struct device *dev,
485 	struct device_attribute *attr, char *buf)
486 {
487 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
488 	struct tsl2583_chip *chip = iio_priv(indio_dev);
489 
490 	return sprintf(buf, "%d\n", chip->taos_chip_status);
491 }
492 
taos_power_state_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)493 static ssize_t taos_power_state_store(struct device *dev,
494 	struct device_attribute *attr, const char *buf, size_t len)
495 {
496 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
497 	int value;
498 
499 	if (kstrtoint(buf, 0, &value))
500 		return -EINVAL;
501 
502 	if (!value)
503 		taos_chip_off(indio_dev);
504 	else
505 		taos_chip_on(indio_dev);
506 
507 	return len;
508 }
509 
taos_gain_show(struct device * dev,struct device_attribute * attr,char * buf)510 static ssize_t taos_gain_show(struct device *dev,
511 	struct device_attribute *attr, char *buf)
512 {
513 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
514 	struct tsl2583_chip *chip = iio_priv(indio_dev);
515 	char gain[4] = {0};
516 
517 	switch (chip->taos_settings.als_gain) {
518 	case 0:
519 		strcpy(gain, "001");
520 		break;
521 	case 1:
522 		strcpy(gain, "008");
523 		break;
524 	case 2:
525 		strcpy(gain, "016");
526 		break;
527 	case 3:
528 		strcpy(gain, "111");
529 		break;
530 	}
531 
532 	return sprintf(buf, "%s\n", gain);
533 }
534 
taos_gain_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)535 static ssize_t taos_gain_store(struct device *dev,
536 	struct device_attribute *attr, const char *buf, size_t len)
537 {
538 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
539 	struct tsl2583_chip *chip = iio_priv(indio_dev);
540 	int value;
541 
542 	if (kstrtoint(buf, 0, &value))
543 		return -EINVAL;
544 
545 	switch (value) {
546 	case 1:
547 		chip->taos_settings.als_gain = 0;
548 		break;
549 	case 8:
550 		chip->taos_settings.als_gain = 1;
551 		break;
552 	case 16:
553 		chip->taos_settings.als_gain = 2;
554 		break;
555 	case 111:
556 		chip->taos_settings.als_gain = 3;
557 		break;
558 	default:
559 		dev_err(dev, "Invalid Gain Index (must be 1,8,16,111)\n");
560 		return -1;
561 	}
562 
563 	return len;
564 }
565 
taos_gain_available_show(struct device * dev,struct device_attribute * attr,char * buf)566 static ssize_t taos_gain_available_show(struct device *dev,
567 	struct device_attribute *attr, char *buf)
568 {
569 	return sprintf(buf, "%s\n", "1 8 16 111");
570 }
571 
taos_als_time_show(struct device * dev,struct device_attribute * attr,char * buf)572 static ssize_t taos_als_time_show(struct device *dev,
573 	struct device_attribute *attr, char *buf)
574 {
575 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
576 	struct tsl2583_chip *chip = iio_priv(indio_dev);
577 
578 	return sprintf(buf, "%d\n", chip->taos_settings.als_time);
579 }
580 
taos_als_time_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)581 static ssize_t taos_als_time_store(struct device *dev,
582 	struct device_attribute *attr, const char *buf, size_t len)
583 {
584 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
585 	struct tsl2583_chip *chip = iio_priv(indio_dev);
586 	int value;
587 
588 	if (kstrtoint(buf, 0, &value))
589 		return -EINVAL;
590 
591 	if ((value < 50) || (value > 650))
592 		return -EINVAL;
593 
594 	if (value % 50)
595 		return -EINVAL;
596 
597 	chip->taos_settings.als_time = value;
598 
599 	return len;
600 }
601 
taos_als_time_available_show(struct device * dev,struct device_attribute * attr,char * buf)602 static ssize_t taos_als_time_available_show(struct device *dev,
603 	struct device_attribute *attr, char *buf)
604 {
605 	return sprintf(buf, "%s\n",
606 		"50 100 150 200 250 300 350 400 450 500 550 600 650");
607 }
608 
taos_als_trim_show(struct device * dev,struct device_attribute * attr,char * buf)609 static ssize_t taos_als_trim_show(struct device *dev,
610 	struct device_attribute *attr, char *buf)
611 {
612 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
613 	struct tsl2583_chip *chip = iio_priv(indio_dev);
614 
615 	return sprintf(buf, "%d\n", chip->taos_settings.als_gain_trim);
616 }
617 
taos_als_trim_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)618 static ssize_t taos_als_trim_store(struct device *dev,
619 	struct device_attribute *attr, const char *buf, size_t len)
620 {
621 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
622 	struct tsl2583_chip *chip = iio_priv(indio_dev);
623 	int value;
624 
625 	if (kstrtoint(buf, 0, &value))
626 		return -EINVAL;
627 
628 	if (value)
629 		chip->taos_settings.als_gain_trim = value;
630 
631 	return len;
632 }
633 
taos_als_cal_target_show(struct device * dev,struct device_attribute * attr,char * buf)634 static ssize_t taos_als_cal_target_show(struct device *dev,
635 	struct device_attribute *attr, char *buf)
636 {
637 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
638 	struct tsl2583_chip *chip = iio_priv(indio_dev);
639 
640 	return sprintf(buf, "%d\n", chip->taos_settings.als_cal_target);
641 }
642 
taos_als_cal_target_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)643 static ssize_t taos_als_cal_target_store(struct device *dev,
644 	struct device_attribute *attr, const char *buf, size_t len)
645 {
646 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
647 	struct tsl2583_chip *chip = iio_priv(indio_dev);
648 	int value;
649 
650 	if (kstrtoint(buf, 0, &value))
651 		return -EINVAL;
652 
653 	if (value)
654 		chip->taos_settings.als_cal_target = value;
655 
656 	return len;
657 }
658 
taos_lux_show(struct device * dev,struct device_attribute * attr,char * buf)659 static ssize_t taos_lux_show(struct device *dev, struct device_attribute *attr,
660 	char *buf)
661 {
662 	int ret;
663 
664 	ret = taos_get_lux(dev_to_iio_dev(dev));
665 	if (ret < 0)
666 		return ret;
667 
668 	return sprintf(buf, "%d\n", ret);
669 }
670 
taos_do_calibrate(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)671 static ssize_t taos_do_calibrate(struct device *dev,
672 	struct device_attribute *attr, const char *buf, size_t len)
673 {
674 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
675 	int value;
676 
677 	if (kstrtoint(buf, 0, &value))
678 		return -EINVAL;
679 
680 	if (value == 1)
681 		taos_als_calibrate(indio_dev);
682 
683 	return len;
684 }
685 
taos_luxtable_show(struct device * dev,struct device_attribute * attr,char * buf)686 static ssize_t taos_luxtable_show(struct device *dev,
687 	struct device_attribute *attr, char *buf)
688 {
689 	int i;
690 	int offset = 0;
691 
692 	for (i = 0; i < ARRAY_SIZE(taos_device_lux); i++) {
693 		offset += sprintf(buf + offset, "%u,%u,%u,",
694 				  taos_device_lux[i].ratio,
695 				  taos_device_lux[i].ch0,
696 				  taos_device_lux[i].ch1);
697 		if (taos_device_lux[i].ratio == 0) {
698 			/* We just printed the first "0" entry.
699 			 * Now get rid of the extra "," and break. */
700 			offset--;
701 			break;
702 		}
703 	}
704 
705 	offset += sprintf(buf + offset, "\n");
706 	return offset;
707 }
708 
taos_luxtable_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)709 static ssize_t taos_luxtable_store(struct device *dev,
710 	struct device_attribute *attr, const char *buf, size_t len)
711 {
712 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
713 	struct tsl2583_chip *chip = iio_priv(indio_dev);
714 	int value[ARRAY_SIZE(taos_device_lux)*3 + 1];
715 	int n;
716 
717 	get_options(buf, ARRAY_SIZE(value), value);
718 
719 	/* We now have an array of ints starting at value[1], and
720 	 * enumerated by value[0].
721 	 * We expect each group of three ints is one table entry,
722 	 * and the last table entry is all 0.
723 	 */
724 	n = value[0];
725 	if ((n % 3) || n < 6 || n > ((ARRAY_SIZE(taos_device_lux) - 1) * 3)) {
726 		dev_info(dev, "LUX TABLE INPUT ERROR 1 Value[0]=%d\n", n);
727 		return -EINVAL;
728 	}
729 	if ((value[(n - 2)] | value[(n - 1)] | value[n]) != 0) {
730 		dev_info(dev, "LUX TABLE INPUT ERROR 2 Value[0]=%d\n", n);
731 		return -EINVAL;
732 	}
733 
734 	if (chip->taos_chip_status == TSL258X_CHIP_WORKING)
735 		taos_chip_off(indio_dev);
736 
737 	/* Zero out the table */
738 	memset(taos_device_lux, 0, sizeof(taos_device_lux));
739 	memcpy(taos_device_lux, &value[1], (value[0] * 4));
740 
741 	taos_chip_on(indio_dev);
742 
743 	return len;
744 }
745 
746 static DEVICE_ATTR(power_state, S_IRUGO | S_IWUSR,
747 		taos_power_state_show, taos_power_state_store);
748 
749 static DEVICE_ATTR(illuminance0_calibscale, S_IRUGO | S_IWUSR,
750 		taos_gain_show, taos_gain_store);
751 static DEVICE_ATTR(illuminance0_calibscale_available, S_IRUGO,
752 		taos_gain_available_show, NULL);
753 
754 static DEVICE_ATTR(illuminance0_integration_time, S_IRUGO | S_IWUSR,
755 		taos_als_time_show, taos_als_time_store);
756 static DEVICE_ATTR(illuminance0_integration_time_available, S_IRUGO,
757 		taos_als_time_available_show, NULL);
758 
759 static DEVICE_ATTR(illuminance0_calibbias, S_IRUGO | S_IWUSR,
760 		taos_als_trim_show, taos_als_trim_store);
761 
762 static DEVICE_ATTR(illuminance0_input_target, S_IRUGO | S_IWUSR,
763 		taos_als_cal_target_show, taos_als_cal_target_store);
764 
765 static DEVICE_ATTR(illuminance0_input, S_IRUGO, taos_lux_show, NULL);
766 static DEVICE_ATTR(illuminance0_calibrate, S_IWUSR, NULL, taos_do_calibrate);
767 static DEVICE_ATTR(illuminance0_lux_table, S_IRUGO | S_IWUSR,
768 		taos_luxtable_show, taos_luxtable_store);
769 
770 static struct attribute *sysfs_attrs_ctrl[] = {
771 	&dev_attr_power_state.attr,
772 	&dev_attr_illuminance0_calibscale.attr,			/* Gain  */
773 	&dev_attr_illuminance0_calibscale_available.attr,
774 	&dev_attr_illuminance0_integration_time.attr,	/* I time*/
775 	&dev_attr_illuminance0_integration_time_available.attr,
776 	&dev_attr_illuminance0_calibbias.attr,			/* trim  */
777 	&dev_attr_illuminance0_input_target.attr,
778 	&dev_attr_illuminance0_input.attr,
779 	&dev_attr_illuminance0_calibrate.attr,
780 	&dev_attr_illuminance0_lux_table.attr,
781 	NULL
782 };
783 
784 static struct attribute_group tsl2583_attribute_group = {
785 	.attrs = sysfs_attrs_ctrl,
786 };
787 
788 /* Use the default register values to identify the Taos device */
taos_tsl258x_device(unsigned char * bufp)789 static int taos_tsl258x_device(unsigned char *bufp)
790 {
791 	return ((bufp[TSL258X_CHIPID] & 0xf0) == 0x90);
792 }
793 
794 static const struct iio_info tsl2583_info = {
795 	.attrs = &tsl2583_attribute_group,
796 	.driver_module = THIS_MODULE,
797 };
798 
799 /*
800  * Client probe function - When a valid device is found, the driver's device
801  * data structure is updated, and initialization completes successfully.
802  */
taos_probe(struct i2c_client * clientp,const struct i2c_device_id * idp)803 static int taos_probe(struct i2c_client *clientp,
804 		      const struct i2c_device_id *idp)
805 {
806 	int i, ret;
807 	unsigned char buf[TSL258X_MAX_DEVICE_REGS];
808 	struct tsl2583_chip *chip;
809 	struct iio_dev *indio_dev;
810 
811 	if (!i2c_check_functionality(clientp->adapter,
812 		I2C_FUNC_SMBUS_BYTE_DATA)) {
813 		dev_err(&clientp->dev, "taos_probe() - i2c smbus byte data func unsupported\n");
814 		return -EOPNOTSUPP;
815 	}
816 
817 	indio_dev = devm_iio_device_alloc(&clientp->dev, sizeof(*chip));
818 	if (!indio_dev)
819 		return -ENOMEM;
820 	chip = iio_priv(indio_dev);
821 	chip->client = clientp;
822 	i2c_set_clientdata(clientp, indio_dev);
823 
824 	mutex_init(&chip->als_mutex);
825 	chip->taos_chip_status = TSL258X_CHIP_UNKNOWN;
826 	memcpy(chip->taos_config, taos_config, sizeof(chip->taos_config));
827 
828 	for (i = 0; i < TSL258X_MAX_DEVICE_REGS; i++) {
829 		ret = i2c_smbus_write_byte(clientp,
830 				(TSL258X_CMD_REG | (TSL258X_CNTRL + i)));
831 		if (ret < 0) {
832 			dev_err(&clientp->dev,
833 				"i2c_smbus_write_byte to cmd reg failed in taos_probe(), err = %d\n",
834 				ret);
835 			return ret;
836 		}
837 		ret = i2c_smbus_read_byte(clientp);
838 		if (ret < 0) {
839 			dev_err(&clientp->dev,
840 				"i2c_smbus_read_byte from reg failed in taos_probe(), err = %d\n",
841 				ret);
842 			return ret;
843 		}
844 		buf[i] = ret;
845 	}
846 
847 	if (!taos_tsl258x_device(buf)) {
848 		dev_info(&clientp->dev,
849 			"i2c device found but does not match expected id in taos_probe()\n");
850 		return -EINVAL;
851 	}
852 
853 	ret = i2c_smbus_write_byte(clientp, (TSL258X_CMD_REG | TSL258X_CNTRL));
854 	if (ret < 0) {
855 		dev_err(&clientp->dev,
856 			"i2c_smbus_write_byte() to cmd reg failed in taos_probe(), err = %d\n",
857 			ret);
858 		return ret;
859 	}
860 
861 	indio_dev->info = &tsl2583_info;
862 	indio_dev->dev.parent = &clientp->dev;
863 	indio_dev->modes = INDIO_DIRECT_MODE;
864 	indio_dev->name = chip->client->name;
865 	ret = devm_iio_device_register(indio_dev->dev.parent, indio_dev);
866 	if (ret) {
867 		dev_err(&clientp->dev, "iio registration failed\n");
868 		return ret;
869 	}
870 
871 	/* Load up the V2 defaults (these are hard coded defaults for now) */
872 	taos_defaults(chip);
873 
874 	/* Make sure the chip is on */
875 	taos_chip_on(indio_dev);
876 
877 	dev_info(&clientp->dev, "Light sensor found.\n");
878 	return 0;
879 }
880 
881 #ifdef CONFIG_PM_SLEEP
taos_suspend(struct device * dev)882 static int taos_suspend(struct device *dev)
883 {
884 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
885 	struct tsl2583_chip *chip = iio_priv(indio_dev);
886 	int ret = 0;
887 
888 	mutex_lock(&chip->als_mutex);
889 
890 	if (chip->taos_chip_status == TSL258X_CHIP_WORKING) {
891 		ret = taos_chip_off(indio_dev);
892 		chip->taos_chip_status = TSL258X_CHIP_SUSPENDED;
893 	}
894 
895 	mutex_unlock(&chip->als_mutex);
896 	return ret;
897 }
898 
taos_resume(struct device * dev)899 static int taos_resume(struct device *dev)
900 {
901 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
902 	struct tsl2583_chip *chip = iio_priv(indio_dev);
903 	int ret = 0;
904 
905 	mutex_lock(&chip->als_mutex);
906 
907 	if (chip->taos_chip_status == TSL258X_CHIP_SUSPENDED)
908 		ret = taos_chip_on(indio_dev);
909 
910 	mutex_unlock(&chip->als_mutex);
911 	return ret;
912 }
913 
914 static SIMPLE_DEV_PM_OPS(taos_pm_ops, taos_suspend, taos_resume);
915 #define TAOS_PM_OPS (&taos_pm_ops)
916 #else
917 #define TAOS_PM_OPS NULL
918 #endif
919 
920 static struct i2c_device_id taos_idtable[] = {
921 	{ "tsl2580", 0 },
922 	{ "tsl2581", 1 },
923 	{ "tsl2583", 2 },
924 	{}
925 };
926 MODULE_DEVICE_TABLE(i2c, taos_idtable);
927 
928 /* Driver definition */
929 static struct i2c_driver taos_driver = {
930 	.driver = {
931 		.name = "tsl2583",
932 		.pm = TAOS_PM_OPS,
933 	},
934 	.id_table = taos_idtable,
935 	.probe = taos_probe,
936 };
937 module_i2c_driver(taos_driver);
938 
939 MODULE_AUTHOR("J. August Brenner<jbrenner@taosinc.com>");
940 MODULE_DESCRIPTION("TAOS tsl2583 ambient light sensor driver");
941 MODULE_LICENSE("GPL");
942