blob: fb711ed4862e1aa99d0dec658355f431089a105e [file] [log] [blame]
Jon Brennerac4f6ee2011-04-15 18:38:43 +01001/*
2 * Device driver for monitoring ambient light intensity (lux)
Brian Masneyaadc4c92016-11-12 13:19:35 -05003 * within the TAOS tsl258x family of devices (tsl2580, tsl2581, tsl2583).
Jon Brennerac4f6ee2011-04-15 18:38:43 +01004 *
5 * Copyright (c) 2011, TAOS Corporation.
Brian Masney371894f2017-04-25 04:06:33 -04006 * Copyright (c) 2016-2017 Brian Masney <masneyb@onstation.org>
Jon Brennerac4f6ee2011-04-15 18:38:43 +01007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * more details.
Jon Brennerac4f6ee2011-04-15 18:38:43 +010017 */
18
19#include <linux/kernel.h>
20#include <linux/i2c.h>
21#include <linux/errno.h>
22#include <linux/delay.h>
23#include <linux/string.h>
24#include <linux/mutex.h>
25#include <linux/unistd.h>
26#include <linux/slab.h>
Paul Gortmaker99c97852011-07-03 15:49:50 -040027#include <linux/module.h>
Jonathan Cameron06458e22012-04-25 15:54:58 +010028#include <linux/iio/iio.h>
Brian Masneyb2fa81b2016-10-28 06:00:19 -040029#include <linux/iio/sysfs.h>
Brian Masney371894f2017-04-25 04:06:33 -040030#include <linux/pm_runtime.h>
Jon Brennerac4f6ee2011-04-15 18:38:43 +010031
Jon Brennerac4f6ee2011-04-15 18:38:43 +010032/* Device Registers and Masks */
Brian Masney686c5922016-11-12 13:19:23 -050033#define TSL2583_CNTRL 0x00
34#define TSL2583_ALS_TIME 0X01
35#define TSL2583_INTERRUPT 0x02
36#define TSL2583_GAIN 0x07
37#define TSL2583_REVID 0x11
38#define TSL2583_CHIPID 0x12
39#define TSL2583_ALS_CHAN0LO 0x14
40#define TSL2583_ALS_CHAN0HI 0x15
41#define TSL2583_ALS_CHAN1LO 0x16
42#define TSL2583_ALS_CHAN1HI 0x17
43#define TSL2583_TMR_LO 0x18
44#define TSL2583_TMR_HI 0x19
Jon Brennerac4f6ee2011-04-15 18:38:43 +010045
46/* tsl2583 cmd reg masks */
Brian Masney686c5922016-11-12 13:19:23 -050047#define TSL2583_CMD_REG 0x80
48#define TSL2583_CMD_SPL_FN 0x60
Brian Masney8391c882016-11-12 13:19:24 -050049#define TSL2583_CMD_ALS_INT_CLR 0x01
Jon Brennerac4f6ee2011-04-15 18:38:43 +010050
51/* tsl2583 cntrl reg masks */
Brian Masney8391c882016-11-12 13:19:24 -050052#define TSL2583_CNTL_ADC_ENBL 0x02
Brian Masney686c5922016-11-12 13:19:23 -050053#define TSL2583_CNTL_PWR_OFF 0x00
54#define TSL2583_CNTL_PWR_ON 0x01
Jon Brennerac4f6ee2011-04-15 18:38:43 +010055
56/* tsl2583 status reg masks */
Brian Masney8391c882016-11-12 13:19:24 -050057#define TSL2583_STA_ADC_VALID 0x01
58#define TSL2583_STA_ADC_INTR 0x10
Jon Brennerac4f6ee2011-04-15 18:38:43 +010059
60/* Lux calculation constants */
Brian Masney8391c882016-11-12 13:19:24 -050061#define TSL2583_LUX_CALC_OVER_FLOW 65535
Jon Brennerac4f6ee2011-04-15 18:38:43 +010062
Brian Masneyc908fb72016-11-10 04:25:37 -050063#define TSL2583_INTERRUPT_DISABLED 0x00
64
Brian Masney6bd0cb22016-11-03 08:56:13 -040065#define TSL2583_CHIP_ID 0x90
66#define TSL2583_CHIP_ID_MASK 0xf0
67
Brian Masney371894f2017-04-25 04:06:33 -040068#define TSL2583_POWER_OFF_DELAY_MS 2000
69
Jon Brennerac4f6ee2011-04-15 18:38:43 +010070/* Per-device data */
Brian Masney686c5922016-11-12 13:19:23 -050071struct tsl2583_als_info {
Jon Brennerac4f6ee2011-04-15 18:38:43 +010072 u16 als_ch0;
73 u16 als_ch1;
74 u16 lux;
75};
76
Brian Masney0b6b3612016-11-12 13:19:34 -050077struct tsl2583_lux {
78 unsigned int ratio;
79 unsigned int ch0;
80 unsigned int ch1;
81};
82
83static const struct tsl2583_lux tsl2583_default_lux[] = {
84 { 9830, 8520, 15729 },
85 { 12452, 10807, 23344 },
86 { 14746, 6383, 11705 },
87 { 17695, 4063, 6554 },
88 { 0, 0, 0 } /* Termination segment */
89};
90
91#define TSL2583_MAX_LUX_TABLE_ENTRIES 11
92
Brian Masney686c5922016-11-12 13:19:23 -050093struct tsl2583_settings {
Jon Brennerac4f6ee2011-04-15 18:38:43 +010094 int als_time;
95 int als_gain;
96 int als_gain_trim;
97 int als_cal_target;
Brian Masney0b6b3612016-11-12 13:19:34 -050098
99 /*
100 * This structure is intentionally large to accommodate updates via
101 * sysfs. Sized to 11 = max 10 segments + 1 termination segment.
102 * Assumption is that one and only one type of glass used.
103 */
104 struct tsl2583_lux als_device_lux[TSL2583_MAX_LUX_TABLE_ENTRIES];
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100105};
106
107struct tsl2583_chip {
108 struct mutex als_mutex;
109 struct i2c_client *client;
Brian Masney686c5922016-11-12 13:19:23 -0500110 struct tsl2583_als_info als_cur_info;
111 struct tsl2583_settings als_settings;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100112 int als_time_scale;
113 int als_saturation;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100114};
115
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100116struct gainadj {
117 s16 ch0;
118 s16 ch1;
Brian Masney143c30f2016-10-28 06:00:18 -0400119 s16 mean;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100120};
121
122/* Index = (0 - 3) Used to validate the gain selection index */
123static const struct gainadj gainadj[] = {
Brian Masney143c30f2016-10-28 06:00:18 -0400124 { 1, 1, 1 },
125 { 8, 8, 8 },
126 { 16, 16, 16 },
127 { 107, 115, 111 }
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100128};
129
130/*
131 * Provides initial operational parameter defaults.
132 * These defaults may be changed through the device's sysfs files.
133 */
Brian Masney686c5922016-11-12 13:19:23 -0500134static void tsl2583_defaults(struct tsl2583_chip *chip)
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100135{
Brian Masney4a0f3612016-11-03 08:56:14 -0400136 /*
137 * The integration time must be a multiple of 50ms and within the
138 * range [50, 600] ms.
139 */
Brian Masney686c5922016-11-12 13:19:23 -0500140 chip->als_settings.als_time = 100;
Brian Masney4a0f3612016-11-03 08:56:14 -0400141
142 /*
143 * This is an index into the gainadj table. Assume clear glass as the
144 * default.
145 */
Brian Masney686c5922016-11-12 13:19:23 -0500146 chip->als_settings.als_gain = 0;
Brian Masney4a0f3612016-11-03 08:56:14 -0400147
148 /* Default gain trim to account for aperture effects */
Brian Masney686c5922016-11-12 13:19:23 -0500149 chip->als_settings.als_gain_trim = 1000;
Brian Masney4a0f3612016-11-03 08:56:14 -0400150
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100151 /* Known external ALS reading used for calibration */
Brian Masney686c5922016-11-12 13:19:23 -0500152 chip->als_settings.als_cal_target = 130;
Brian Masney0b6b3612016-11-12 13:19:34 -0500153
154 /* Default lux table. */
155 memcpy(chip->als_settings.als_device_lux, tsl2583_default_lux,
156 sizeof(tsl2583_default_lux));
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100157}
158
159/*
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100160 * Reads and calculates current lux value.
161 * The raw ch0 and ch1 values of the ambient light sensed in the last
162 * integration cycle are read from the device.
163 * Time scale factor array values are adjusted based on the integration time.
164 * The raw values are multiplied by a scale factor, and device gain is obtained
165 * using gain index. Limit checks are done next, then the ratio of a multiple
Brian Masney0b6b3612016-11-12 13:19:34 -0500166 * of ch1 value, to the ch0 value, is calculated. The array als_device_lux[]
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100167 * declared above is then scanned to find the first ratio value that is just
168 * above the ratio we just calculated. The ch0 and ch1 multiplier constants in
169 * the array are then used along with the time scale factor array values, to
170 * calculate the lux.
171 */
Brian Masney686c5922016-11-12 13:19:23 -0500172static int tsl2583_get_lux(struct iio_dev *indio_dev)
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100173{
174 u16 ch0, ch1; /* separated ch0/ch1 data from device */
175 u32 lux; /* raw lux calculated from device data */
Bryan Freed014fcb12011-12-02 14:19:30 -0800176 u64 lux64;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100177 u32 ratio;
178 u8 buf[5];
Brian Masney686c5922016-11-12 13:19:23 -0500179 struct tsl2583_lux *p;
Jonathan Camerone05e5f22011-08-12 17:08:52 +0100180 struct tsl2583_chip *chip = iio_priv(indio_dev);
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100181 int i, ret;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100182
Brian Masney686c5922016-11-12 13:19:23 -0500183 ret = i2c_smbus_read_byte_data(chip->client, TSL2583_CMD_REG);
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100184 if (ret < 0) {
Brian Masney24ad4302016-11-12 13:19:22 -0500185 dev_err(&chip->client->dev, "%s: failed to read CMD_REG register\n",
186 __func__);
Brian Masneyb3d94152016-10-28 06:00:17 -0400187 goto done;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100188 }
Brian Masney6ba5dee2016-11-03 08:56:12 -0400189
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100190 /* is data new & valid */
Brian Masney686c5922016-11-12 13:19:23 -0500191 if (!(ret & TSL2583_STA_ADC_INTR)) {
Brian Masney24ad4302016-11-12 13:19:22 -0500192 dev_err(&chip->client->dev, "%s: data not valid; returning last value\n",
193 __func__);
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100194 ret = chip->als_cur_info.lux; /* return LAST VALUE */
Brian Masneyb3d94152016-10-28 06:00:17 -0400195 goto done;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100196 }
197
198 for (i = 0; i < 4; i++) {
Brian Masney686c5922016-11-12 13:19:23 -0500199 int reg = TSL2583_CMD_REG | (TSL2583_ALS_CHAN0LO + i);
Aybuke Ozdemirf6077f42014-09-24 23:13:21 +0300200
Brian Masney6ba5dee2016-11-03 08:56:12 -0400201 ret = i2c_smbus_read_byte_data(chip->client, reg);
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100202 if (ret < 0) {
Brian Masney24ad4302016-11-12 13:19:22 -0500203 dev_err(&chip->client->dev, "%s: failed to read register %x\n",
204 __func__, reg);
Brian Masneyb3d94152016-10-28 06:00:17 -0400205 goto done;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100206 }
Brian Masney6ba5dee2016-11-03 08:56:12 -0400207 buf[i] = ret;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100208 }
209
Eva Rachel Retuya7f3829a2016-02-18 15:35:38 +0800210 /*
Brian Masney2fb28482016-11-12 13:19:36 -0500211 * Clear the pending interrupt status bit on the chip to allow the next
212 * integration cycle to start. This has to be done even though this
213 * driver currently does not support interrupts.
Eva Rachel Retuya7f3829a2016-02-18 15:35:38 +0800214 */
Jonathan Camerone05e5f22011-08-12 17:08:52 +0100215 ret = i2c_smbus_write_byte(chip->client,
Brian Masney686c5922016-11-12 13:19:23 -0500216 (TSL2583_CMD_REG | TSL2583_CMD_SPL_FN |
217 TSL2583_CMD_ALS_INT_CLR));
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100218 if (ret < 0) {
Brian Masney24ad4302016-11-12 13:19:22 -0500219 dev_err(&chip->client->dev, "%s: failed to clear the interrupt bit\n",
220 __func__);
Brian Masneyb3d94152016-10-28 06:00:17 -0400221 goto done; /* have no data, so return failure */
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100222 }
223
224 /* extract ALS/lux data */
225 ch0 = le16_to_cpup((const __le16 *)&buf[0]);
226 ch1 = le16_to_cpup((const __le16 *)&buf[2]);
227
228 chip->als_cur_info.als_ch0 = ch0;
229 chip->als_cur_info.als_ch1 = ch1;
230
231 if ((ch0 >= chip->als_saturation) || (ch1 >= chip->als_saturation))
232 goto return_max;
233
Cristina Morarud59b7b62015-10-20 22:55:40 +0300234 if (!ch0) {
Brian Masney043c1da2016-11-12 13:19:29 -0500235 /*
236 * The sensor appears to be in total darkness so set the
237 * calculated lux to 0 and return early to avoid a division by
238 * zero below when calculating the ratio.
239 */
Eva Rachel Retuyaddab4a02016-02-18 15:35:40 +0800240 ret = 0;
241 chip->als_cur_info.lux = 0;
Brian Masneyb3d94152016-10-28 06:00:17 -0400242 goto done;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100243 }
Brian Masney184f9162016-11-12 13:19:26 -0500244
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100245 /* calculate ratio */
246 ratio = (ch1 << 15) / ch0;
Brian Masney184f9162016-11-12 13:19:26 -0500247
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100248 /* convert to unscaled lux using the pointer to the table */
Brian Masney0b6b3612016-11-12 13:19:34 -0500249 for (p = (struct tsl2583_lux *)chip->als_settings.als_device_lux;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100250 p->ratio != 0 && p->ratio < ratio; p++)
251 ;
252
253 if (p->ratio == 0) {
254 lux = 0;
255 } else {
Brian Masneyed912552016-11-12 13:19:30 -0500256 u32 ch0lux, ch1lux;
257
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100258 ch0lux = ((ch0 * p->ch0) +
Brian Masney686c5922016-11-12 13:19:23 -0500259 (gainadj[chip->als_settings.als_gain].ch0 >> 1))
260 / gainadj[chip->als_settings.als_gain].ch0;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100261 ch1lux = ((ch1 * p->ch1) +
Brian Masney686c5922016-11-12 13:19:23 -0500262 (gainadj[chip->als_settings.als_gain].ch1 >> 1))
263 / gainadj[chip->als_settings.als_gain].ch1;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100264
Brian Masneyed912552016-11-12 13:19:30 -0500265 /* note: lux is 31 bit max at this point */
266 if (ch1lux > ch0lux) {
267 dev_dbg(&chip->client->dev, "%s: No Data - Returning 0\n",
268 __func__);
269 ret = 0;
270 chip->als_cur_info.lux = 0;
271 goto done;
272 }
273
274 lux = ch0lux - ch1lux;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100275 }
276
277 /* adjust for active time scale */
278 if (chip->als_time_scale == 0)
279 lux = 0;
280 else
281 lux = (lux + (chip->als_time_scale >> 1)) /
282 chip->als_time_scale;
283
Brian Masney2a1e3f02016-11-12 13:19:28 -0500284 /*
285 * Adjust for active gain scale.
Brian Masney0b6b3612016-11-12 13:19:34 -0500286 * The tsl2583_default_lux tables above have a factor of 8192 built in,
Bryan Freed014fcb12011-12-02 14:19:30 -0800287 * so we need to shift right.
288 * User-specified gain provides a multiplier.
289 * Apply user-specified gain before shifting right to retain precision.
290 * Use 64 bits to avoid overflow on multiplication.
291 * Then go back to 32 bits before division to avoid using div_u64().
292 */
293 lux64 = lux;
Brian Masney686c5922016-11-12 13:19:23 -0500294 lux64 = lux64 * chip->als_settings.als_gain_trim;
Bryan Freed014fcb12011-12-02 14:19:30 -0800295 lux64 >>= 13;
296 lux = lux64;
297 lux = (lux + 500) / 1000;
Brian Masney184f9162016-11-12 13:19:26 -0500298
Brian Masney686c5922016-11-12 13:19:23 -0500299 if (lux > TSL2583_LUX_CALC_OVER_FLOW) { /* check for overflow */
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100300return_max:
Brian Masney686c5922016-11-12 13:19:23 -0500301 lux = TSL2583_LUX_CALC_OVER_FLOW;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100302 }
303
304 /* Update the structure with the latest VALID lux. */
305 chip->als_cur_info.lux = lux;
306 ret = lux;
307
Brian Masneyb3d94152016-10-28 06:00:17 -0400308done:
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100309 return ret;
310}
311
312/*
313 * Obtain single reading and calculate the als_gain_trim (later used
314 * to derive actual lux).
315 * Return updated gain_trim value.
316 */
Brian Masney686c5922016-11-12 13:19:23 -0500317static int tsl2583_als_calibrate(struct iio_dev *indio_dev)
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100318{
Jonathan Camerone05e5f22011-08-12 17:08:52 +0100319 struct tsl2583_chip *chip = iio_priv(indio_dev);
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100320 unsigned int gain_trim_val;
321 int ret;
322 int lux_val;
323
Brian Masney6ba5dee2016-11-03 08:56:12 -0400324 ret = i2c_smbus_read_byte_data(chip->client,
Brian Masney686c5922016-11-12 13:19:23 -0500325 TSL2583_CMD_REG | TSL2583_CNTRL);
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100326 if (ret < 0) {
Jonathan Camerone05e5f22011-08-12 17:08:52 +0100327 dev_err(&chip->client->dev,
Brian Masney24ad4302016-11-12 13:19:22 -0500328 "%s: failed to read from the CNTRL register\n",
Brian Masney9d5f36d2016-10-28 06:00:13 -0400329 __func__);
330 return ret;
331 }
332
Brian Masney686c5922016-11-12 13:19:23 -0500333 if ((ret & (TSL2583_CNTL_ADC_ENBL | TSL2583_CNTL_PWR_ON))
334 != (TSL2583_CNTL_ADC_ENBL | TSL2583_CNTL_PWR_ON)) {
Jonathan Camerone05e5f22011-08-12 17:08:52 +0100335 dev_err(&chip->client->dev,
Brian Masney24ad4302016-11-12 13:19:22 -0500336 "%s: Device is not powered on and/or ADC is not enabled\n",
337 __func__);
Brian Masney20d823a2016-10-28 06:00:14 -0400338 return -EINVAL;
Brian Masney686c5922016-11-12 13:19:23 -0500339 } else if ((ret & TSL2583_STA_ADC_VALID) != TSL2583_STA_ADC_VALID) {
Jonathan Camerone05e5f22011-08-12 17:08:52 +0100340 dev_err(&chip->client->dev,
Brian Masney24ad4302016-11-12 13:19:22 -0500341 "%s: The two ADC channels have not completed an integration cycle\n",
342 __func__);
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100343 return -ENODATA;
344 }
Brian Masney184f9162016-11-12 13:19:26 -0500345
Brian Masney686c5922016-11-12 13:19:23 -0500346 lux_val = tsl2583_get_lux(indio_dev);
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100347 if (lux_val < 0) {
Brian Masney24ad4302016-11-12 13:19:22 -0500348 dev_err(&chip->client->dev, "%s: failed to get lux\n",
349 __func__);
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100350 return lux_val;
351 }
Brian Masney184f9162016-11-12 13:19:26 -0500352
Brian Masney686c5922016-11-12 13:19:23 -0500353 gain_trim_val = (unsigned int)(((chip->als_settings.als_cal_target)
354 * chip->als_settings.als_gain_trim) / lux_val);
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100355 if ((gain_trim_val < 250) || (gain_trim_val > 4000)) {
Jonathan Camerone05e5f22011-08-12 17:08:52 +0100356 dev_err(&chip->client->dev,
Brian Masney24ad4302016-11-12 13:19:22 -0500357 "%s: trim_val of %d is not within the range [250, 4000]\n",
358 __func__, gain_trim_val);
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100359 return -ENODATA;
360 }
Brian Masney184f9162016-11-12 13:19:26 -0500361
Brian Masney686c5922016-11-12 13:19:23 -0500362 chip->als_settings.als_gain_trim = (int)gain_trim_val;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100363
Brian Masneya8898dc2016-11-12 13:19:31 -0500364 return 0;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100365}
366
Brian Masney21677692016-11-10 04:25:38 -0500367static int tsl2583_set_als_time(struct tsl2583_chip *chip)
368{
369 int als_count, als_time, ret;
370 u8 val;
371
372 /* determine als integration register */
Brian Masney686c5922016-11-12 13:19:23 -0500373 als_count = (chip->als_settings.als_time * 100 + 135) / 270;
Brian Masney21677692016-11-10 04:25:38 -0500374 if (!als_count)
375 als_count = 1; /* ensure at least one cycle */
376
377 /* convert back to time (encompasses overrides) */
378 als_time = (als_count * 27 + 5) / 10;
379
380 val = 256 - als_count;
381 ret = i2c_smbus_write_byte_data(chip->client,
Brian Masney686c5922016-11-12 13:19:23 -0500382 TSL2583_CMD_REG | TSL2583_ALS_TIME,
Brian Masney21677692016-11-10 04:25:38 -0500383 val);
384 if (ret < 0) {
Brian Masney24ad4302016-11-12 13:19:22 -0500385 dev_err(&chip->client->dev, "%s: failed to set the als time to %d\n",
Brian Masney21677692016-11-10 04:25:38 -0500386 __func__, val);
387 return ret;
388 }
389
390 /* set chip struct re scaling and saturation */
391 chip->als_saturation = als_count * 922; /* 90% of full scale */
392 chip->als_time_scale = (als_time + 25) / 50;
393
394 return ret;
395}
396
397static int tsl2583_set_als_gain(struct tsl2583_chip *chip)
398{
399 int ret;
400
Brian Masney686c5922016-11-12 13:19:23 -0500401 /* Set the gain based on als_settings struct */
Brian Masney21677692016-11-10 04:25:38 -0500402 ret = i2c_smbus_write_byte_data(chip->client,
Brian Masney686c5922016-11-12 13:19:23 -0500403 TSL2583_CMD_REG | TSL2583_GAIN,
404 chip->als_settings.als_gain);
Brian Masney21677692016-11-10 04:25:38 -0500405 if (ret < 0)
Brian Masney24ad4302016-11-12 13:19:22 -0500406 dev_err(&chip->client->dev,
407 "%s: failed to set the gain to %d\n", __func__,
Brian Masney686c5922016-11-12 13:19:23 -0500408 chip->als_settings.als_gain);
Brian Masney21677692016-11-10 04:25:38 -0500409
410 return ret;
411}
412
Brian Masneyc908fb72016-11-10 04:25:37 -0500413static int tsl2583_set_power_state(struct tsl2583_chip *chip, u8 state)
414{
415 int ret;
416
417 ret = i2c_smbus_write_byte_data(chip->client,
Brian Masney686c5922016-11-12 13:19:23 -0500418 TSL2583_CMD_REG | TSL2583_CNTRL, state);
Brian Masneyc908fb72016-11-10 04:25:37 -0500419 if (ret < 0)
Brian Masney24ad4302016-11-12 13:19:22 -0500420 dev_err(&chip->client->dev,
421 "%s: failed to set the power state to %d\n", __func__,
422 state);
Brian Masneyc908fb72016-11-10 04:25:37 -0500423
424 return ret;
425}
426
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100427/*
428 * Turn the device on.
429 * Configuration must be set before calling this function.
430 */
Brian Masneyc908fb72016-11-10 04:25:37 -0500431static int tsl2583_chip_init_and_power_on(struct iio_dev *indio_dev)
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100432{
Jonathan Camerone05e5f22011-08-12 17:08:52 +0100433 struct tsl2583_chip *chip = iio_priv(indio_dev);
Brian Masney21677692016-11-10 04:25:38 -0500434 int ret;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100435
Brian Masneyc908fb72016-11-10 04:25:37 -0500436 /* Power on the device; ADC off. */
Brian Masney686c5922016-11-12 13:19:23 -0500437 ret = tsl2583_set_power_state(chip, TSL2583_CNTL_PWR_ON);
Brian Masneyc908fb72016-11-10 04:25:37 -0500438 if (ret < 0)
439 return ret;
440
441 ret = i2c_smbus_write_byte_data(chip->client,
Brian Masney686c5922016-11-12 13:19:23 -0500442 TSL2583_CMD_REG | TSL2583_INTERRUPT,
Brian Masneyc908fb72016-11-10 04:25:37 -0500443 TSL2583_INTERRUPT_DISABLED);
444 if (ret < 0) {
Brian Masney24ad4302016-11-12 13:19:22 -0500445 dev_err(&chip->client->dev,
446 "%s: failed to disable interrupts\n", __func__);
Brian Masneyc908fb72016-11-10 04:25:37 -0500447 return ret;
448 }
449
Brian Masney21677692016-11-10 04:25:38 -0500450 ret = tsl2583_set_als_time(chip);
451 if (ret < 0)
Brian Masneyc908fb72016-11-10 04:25:37 -0500452 return ret;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100453
Brian Masney21677692016-11-10 04:25:38 -0500454 ret = tsl2583_set_als_gain(chip);
455 if (ret < 0)
Brian Masneyc908fb72016-11-10 04:25:37 -0500456 return ret;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100457
Michael Welling7fe704c2014-04-09 21:26:45 -0500458 usleep_range(3000, 3500);
Brian Masneyc908fb72016-11-10 04:25:37 -0500459
Brian Masney686c5922016-11-12 13:19:23 -0500460 ret = tsl2583_set_power_state(chip, TSL2583_CNTL_PWR_ON |
461 TSL2583_CNTL_ADC_ENBL);
Brian Masneyc908fb72016-11-10 04:25:37 -0500462 if (ret < 0)
Monam Agarwal67fc0122014-03-09 10:58:40 +0530463 return ret;
Brian Masneyc908fb72016-11-10 04:25:37 -0500464
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100465 return ret;
466}
467
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100468/* Sysfs Interface Functions */
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100469
Brian Masneyb2fa81b2016-10-28 06:00:19 -0400470static ssize_t in_illuminance_input_target_show(struct device *dev,
471 struct device_attribute *attr,
472 char *buf)
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100473{
Lars-Peter Clausen96f691f2012-05-12 15:39:51 +0200474 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Camerone05e5f22011-08-12 17:08:52 +0100475 struct tsl2583_chip *chip = iio_priv(indio_dev);
Brian Masney1b7e9e22016-10-28 06:00:21 -0400476 int ret;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100477
Brian Masney1b7e9e22016-10-28 06:00:21 -0400478 mutex_lock(&chip->als_mutex);
Brian Masney686c5922016-11-12 13:19:23 -0500479 ret = sprintf(buf, "%d\n", chip->als_settings.als_cal_target);
Brian Masney1b7e9e22016-10-28 06:00:21 -0400480 mutex_unlock(&chip->als_mutex);
481
482 return ret;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100483}
484
Brian Masneyb2fa81b2016-10-28 06:00:19 -0400485static ssize_t in_illuminance_input_target_store(struct device *dev,
486 struct device_attribute *attr,
487 const char *buf, size_t len)
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100488{
Lars-Peter Clausen96f691f2012-05-12 15:39:51 +0200489 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Camerone05e5f22011-08-12 17:08:52 +0100490 struct tsl2583_chip *chip = iio_priv(indio_dev);
Jingoo Hane5e26dd2013-08-20 03:31:00 +0100491 int value;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100492
Brian Masneyf375a492016-10-28 06:00:20 -0400493 if (kstrtoint(buf, 0, &value) || !value)
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100494 return -EINVAL;
495
Brian Masney1b7e9e22016-10-28 06:00:21 -0400496 mutex_lock(&chip->als_mutex);
Brian Masney686c5922016-11-12 13:19:23 -0500497 chip->als_settings.als_cal_target = value;
Brian Masney1b7e9e22016-10-28 06:00:21 -0400498 mutex_unlock(&chip->als_mutex);
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100499
500 return len;
501}
502
Brian Masneyb2fa81b2016-10-28 06:00:19 -0400503static ssize_t in_illuminance_calibrate_store(struct device *dev,
504 struct device_attribute *attr,
505 const char *buf, size_t len)
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100506{
Lars-Peter Clausen96f691f2012-05-12 15:39:51 +0200507 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Brian Masney1b7e9e22016-10-28 06:00:21 -0400508 struct tsl2583_chip *chip = iio_priv(indio_dev);
Brian Masneyacf9ead2016-11-12 13:19:16 -0500509 int value, ret;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100510
Brian Masneyf375a492016-10-28 06:00:20 -0400511 if (kstrtoint(buf, 0, &value) || value != 1)
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100512 return -EINVAL;
513
Brian Masney1b7e9e22016-10-28 06:00:21 -0400514 mutex_lock(&chip->als_mutex);
Brian Masneyacf9ead2016-11-12 13:19:16 -0500515
Brian Masney686c5922016-11-12 13:19:23 -0500516 ret = tsl2583_als_calibrate(indio_dev);
Brian Masneyacf9ead2016-11-12 13:19:16 -0500517 if (ret < 0)
518 goto done;
519
520 ret = len;
521done:
Brian Masney1b7e9e22016-10-28 06:00:21 -0400522 mutex_unlock(&chip->als_mutex);
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100523
Brian Masneyacf9ead2016-11-12 13:19:16 -0500524 return ret;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100525}
526
Brian Masneyb2fa81b2016-10-28 06:00:19 -0400527static ssize_t in_illuminance_lux_table_show(struct device *dev,
528 struct device_attribute *attr,
529 char *buf)
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100530{
Brian Masney0b6b3612016-11-12 13:19:34 -0500531 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
532 struct tsl2583_chip *chip = iio_priv(indio_dev);
Brian Masney6d94de62016-11-12 13:19:25 -0500533 unsigned int i;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100534 int offset = 0;
535
Brian Masney0b6b3612016-11-12 13:19:34 -0500536 for (i = 0; i < ARRAY_SIZE(chip->als_settings.als_device_lux); i++) {
Asaf Vertz3a70eb82015-01-20 12:43:38 +0200537 offset += sprintf(buf + offset, "%u,%u,%u,",
Brian Masney0b6b3612016-11-12 13:19:34 -0500538 chip->als_settings.als_device_lux[i].ratio,
539 chip->als_settings.als_device_lux[i].ch0,
540 chip->als_settings.als_device_lux[i].ch1);
541 if (chip->als_settings.als_device_lux[i].ratio == 0) {
Eva Rachel Retuya7f3829a2016-02-18 15:35:38 +0800542 /*
543 * We just printed the first "0" entry.
544 * Now get rid of the extra "," and break.
545 */
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100546 offset--;
547 break;
548 }
549 }
550
551 offset += sprintf(buf + offset, "\n");
Brian Masney184f9162016-11-12 13:19:26 -0500552
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100553 return offset;
554}
555
Brian Masneyb2fa81b2016-10-28 06:00:19 -0400556static ssize_t in_illuminance_lux_table_store(struct device *dev,
557 struct device_attribute *attr,
558 const char *buf, size_t len)
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100559{
Lars-Peter Clausen96f691f2012-05-12 15:39:51 +0200560 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Camerone05e5f22011-08-12 17:08:52 +0100561 struct tsl2583_chip *chip = iio_priv(indio_dev);
Brian Masney0b6b3612016-11-12 13:19:34 -0500562 const unsigned int max_ints = TSL2583_MAX_LUX_TABLE_ENTRIES * 3;
Dan Carpenter0e8d2b02016-11-24 16:38:07 +0300563 int value[TSL2583_MAX_LUX_TABLE_ENTRIES * 3 + 1];
Brian Masney6d94de62016-11-12 13:19:25 -0500564 int ret = -EINVAL;
565 unsigned int n;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100566
Brian Masney1b7e9e22016-10-28 06:00:21 -0400567 mutex_lock(&chip->als_mutex);
568
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100569 get_options(buf, ARRAY_SIZE(value), value);
570
Brian Masney2a1e3f02016-11-12 13:19:28 -0500571 /*
572 * We now have an array of ints starting at value[1], and
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100573 * enumerated by value[0].
574 * We expect each group of three ints is one table entry,
575 * and the last table entry is all 0.
576 */
577 n = value[0];
Brian Masney0b6b3612016-11-12 13:19:34 -0500578 if ((n % 3) || n < 6 || n > max_ints) {
Brian Masney24ad4302016-11-12 13:19:22 -0500579 dev_err(dev,
Brian Masney0b6b3612016-11-12 13:19:34 -0500580 "%s: The number of entries in the lux table must be a multiple of 3 and within the range [6, %d]\n",
581 __func__, max_ints);
Brian Masney84bc1702016-10-28 06:00:16 -0400582 goto done;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100583 }
Brian Masney1ad51362016-11-12 13:19:32 -0500584 if ((value[n - 2] | value[n - 1] | value[n]) != 0) {
Brian Masney24ad4302016-11-12 13:19:22 -0500585 dev_err(dev, "%s: The last 3 entries in the lux table must be zeros.\n",
586 __func__);
Brian Masney84bc1702016-10-28 06:00:16 -0400587 goto done;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100588 }
589
Brian Masney0b6b3612016-11-12 13:19:34 -0500590 memcpy(chip->als_settings.als_device_lux, &value[1],
591 value[0] * sizeof(value[1]));
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100592
Brian Masney84bc1702016-10-28 06:00:16 -0400593 ret = len;
594
595done:
Brian Masney1b7e9e22016-10-28 06:00:21 -0400596 mutex_unlock(&chip->als_mutex);
597
Brian Masney84bc1702016-10-28 06:00:16 -0400598 return ret;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100599}
600
Brian Masneyb2fa81b2016-10-28 06:00:19 -0400601static IIO_CONST_ATTR(in_illuminance_calibscale_available, "1 8 16 111");
602static IIO_CONST_ATTR(in_illuminance_integration_time_available,
603 "0.000050 0.000100 0.000150 0.000200 0.000250 0.000300 0.000350 0.000400 0.000450 0.000500 0.000550 0.000600 0.000650");
604static IIO_DEVICE_ATTR_RW(in_illuminance_input_target, 0);
605static IIO_DEVICE_ATTR_WO(in_illuminance_calibrate, 0);
606static IIO_DEVICE_ATTR_RW(in_illuminance_lux_table, 0);
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100607
608static struct attribute *sysfs_attrs_ctrl[] = {
Brian Masneyb2fa81b2016-10-28 06:00:19 -0400609 &iio_const_attr_in_illuminance_calibscale_available.dev_attr.attr,
610 &iio_const_attr_in_illuminance_integration_time_available.dev_attr.attr,
611 &iio_dev_attr_in_illuminance_input_target.dev_attr.attr,
612 &iio_dev_attr_in_illuminance_calibrate.dev_attr.attr,
613 &iio_dev_attr_in_illuminance_lux_table.dev_attr.attr,
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100614 NULL
615};
616
Bhumika Goyal58fce732016-09-26 10:31:39 +0530617static const struct attribute_group tsl2583_attribute_group = {
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100618 .attrs = sysfs_attrs_ctrl,
619};
620
Brian Masneyb3d94152016-10-28 06:00:17 -0400621static const struct iio_chan_spec tsl2583_channels[] = {
622 {
623 .type = IIO_LIGHT,
624 .modified = 1,
625 .channel2 = IIO_MOD_LIGHT_IR,
626 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
627 },
628 {
629 .type = IIO_LIGHT,
630 .modified = 1,
631 .channel2 = IIO_MOD_LIGHT_BOTH,
632 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
633 },
634 {
635 .type = IIO_LIGHT,
636 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
637 BIT(IIO_CHAN_INFO_CALIBBIAS) |
Brian Masney143c30f2016-10-28 06:00:18 -0400638 BIT(IIO_CHAN_INFO_CALIBSCALE) |
Brian Masneyb3d94152016-10-28 06:00:17 -0400639 BIT(IIO_CHAN_INFO_INT_TIME),
640 },
641};
642
Brian Masney371894f2017-04-25 04:06:33 -0400643static int tsl2583_set_pm_runtime_busy(struct tsl2583_chip *chip, bool on)
644{
645 int ret;
646
647 if (on) {
648 ret = pm_runtime_get_sync(&chip->client->dev);
649 if (ret < 0)
650 pm_runtime_put_noidle(&chip->client->dev);
651 } else {
652 pm_runtime_mark_last_busy(&chip->client->dev);
653 ret = pm_runtime_put_autosuspend(&chip->client->dev);
654 }
655
656 return ret;
657}
658
Brian Masneyb3d94152016-10-28 06:00:17 -0400659static int tsl2583_read_raw(struct iio_dev *indio_dev,
660 struct iio_chan_spec const *chan,
661 int *val, int *val2, long mask)
662{
663 struct tsl2583_chip *chip = iio_priv(indio_dev);
Brian Masney371894f2017-04-25 04:06:33 -0400664 int ret, pm_ret;
665
666 ret = tsl2583_set_pm_runtime_busy(chip, true);
667 if (ret < 0)
668 return ret;
Brian Masneyb3d94152016-10-28 06:00:17 -0400669
670 mutex_lock(&chip->als_mutex);
671
Brian Masney371894f2017-04-25 04:06:33 -0400672 ret = -EINVAL;
Brian Masneyb3d94152016-10-28 06:00:17 -0400673 switch (mask) {
674 case IIO_CHAN_INFO_RAW:
675 if (chan->type == IIO_LIGHT) {
Brian Masney686c5922016-11-12 13:19:23 -0500676 ret = tsl2583_get_lux(indio_dev);
Brian Masneyb3d94152016-10-28 06:00:17 -0400677 if (ret < 0)
678 goto read_done;
679
680 /*
681 * From page 20 of the TSL2581, TSL2583 data
682 * sheet (TAOS134 − MARCH 2011):
683 *
684 * One of the photodiodes (channel 0) is
685 * sensitive to both visible and infrared light,
686 * while the second photodiode (channel 1) is
687 * sensitive primarily to infrared light.
688 */
689 if (chan->channel2 == IIO_MOD_LIGHT_BOTH)
690 *val = chip->als_cur_info.als_ch0;
691 else
692 *val = chip->als_cur_info.als_ch1;
693
694 ret = IIO_VAL_INT;
695 }
696 break;
697 case IIO_CHAN_INFO_PROCESSED:
698 if (chan->type == IIO_LIGHT) {
Brian Masney686c5922016-11-12 13:19:23 -0500699 ret = tsl2583_get_lux(indio_dev);
Brian Masneyb3d94152016-10-28 06:00:17 -0400700 if (ret < 0)
701 goto read_done;
702
703 *val = ret;
704 ret = IIO_VAL_INT;
705 }
706 break;
707 case IIO_CHAN_INFO_CALIBBIAS:
708 if (chan->type == IIO_LIGHT) {
Brian Masney686c5922016-11-12 13:19:23 -0500709 *val = chip->als_settings.als_gain_trim;
Brian Masneyb3d94152016-10-28 06:00:17 -0400710 ret = IIO_VAL_INT;
711 }
712 break;
Brian Masney143c30f2016-10-28 06:00:18 -0400713 case IIO_CHAN_INFO_CALIBSCALE:
714 if (chan->type == IIO_LIGHT) {
Brian Masney686c5922016-11-12 13:19:23 -0500715 *val = gainadj[chip->als_settings.als_gain].mean;
Brian Masney143c30f2016-10-28 06:00:18 -0400716 ret = IIO_VAL_INT;
717 }
718 break;
Brian Masneyb3d94152016-10-28 06:00:17 -0400719 case IIO_CHAN_INFO_INT_TIME:
720 if (chan->type == IIO_LIGHT) {
721 *val = 0;
Brian Masney686c5922016-11-12 13:19:23 -0500722 *val2 = chip->als_settings.als_time;
Brian Masneyb3d94152016-10-28 06:00:17 -0400723 ret = IIO_VAL_INT_PLUS_MICRO;
724 }
725 break;
726 default:
727 break;
728 }
729
730read_done:
731 mutex_unlock(&chip->als_mutex);
732
Brian Masney371894f2017-04-25 04:06:33 -0400733 if (ret < 0)
734 return ret;
735
736 /*
737 * Preserve the ret variable if the call to
738 * tsl2583_set_pm_runtime_busy() is successful so the reading
739 * (if applicable) is returned to user space.
740 */
741 pm_ret = tsl2583_set_pm_runtime_busy(chip, false);
742 if (pm_ret < 0)
743 return pm_ret;
744
Brian Masneyb3d94152016-10-28 06:00:17 -0400745 return ret;
746}
747
748static int tsl2583_write_raw(struct iio_dev *indio_dev,
749 struct iio_chan_spec const *chan,
750 int val, int val2, long mask)
751{
752 struct tsl2583_chip *chip = iio_priv(indio_dev);
Brian Masney371894f2017-04-25 04:06:33 -0400753 int ret;
754
755 ret = tsl2583_set_pm_runtime_busy(chip, true);
756 if (ret < 0)
757 return ret;
Brian Masneyb3d94152016-10-28 06:00:17 -0400758
759 mutex_lock(&chip->als_mutex);
760
Brian Masney371894f2017-04-25 04:06:33 -0400761 ret = -EINVAL;
Brian Masneyb3d94152016-10-28 06:00:17 -0400762 switch (mask) {
763 case IIO_CHAN_INFO_CALIBBIAS:
764 if (chan->type == IIO_LIGHT) {
Brian Masney686c5922016-11-12 13:19:23 -0500765 chip->als_settings.als_gain_trim = val;
Brian Masneyb3d94152016-10-28 06:00:17 -0400766 ret = 0;
767 }
768 break;
Brian Masney143c30f2016-10-28 06:00:18 -0400769 case IIO_CHAN_INFO_CALIBSCALE:
770 if (chan->type == IIO_LIGHT) {
Brian Masney6d94de62016-11-12 13:19:25 -0500771 unsigned int i;
Brian Masney143c30f2016-10-28 06:00:18 -0400772
773 for (i = 0; i < ARRAY_SIZE(gainadj); i++) {
774 if (gainadj[i].mean == val) {
Brian Masney686c5922016-11-12 13:19:23 -0500775 chip->als_settings.als_gain = i;
Brian Masney21677692016-11-10 04:25:38 -0500776 ret = tsl2583_set_als_gain(chip);
Brian Masney143c30f2016-10-28 06:00:18 -0400777 break;
778 }
779 }
780 }
781 break;
Brian Masneyb3d94152016-10-28 06:00:17 -0400782 case IIO_CHAN_INFO_INT_TIME:
783 if (chan->type == IIO_LIGHT && !val && val2 >= 50 &&
784 val2 <= 650 && !(val2 % 50)) {
Brian Masney686c5922016-11-12 13:19:23 -0500785 chip->als_settings.als_time = val2;
Brian Masney21677692016-11-10 04:25:38 -0500786 ret = tsl2583_set_als_time(chip);
Brian Masneyb3d94152016-10-28 06:00:17 -0400787 }
788 break;
789 default:
790 break;
791 }
792
Brian Masneyb3d94152016-10-28 06:00:17 -0400793 mutex_unlock(&chip->als_mutex);
794
Brian Masney371894f2017-04-25 04:06:33 -0400795 if (ret < 0)
796 return ret;
797
798 ret = tsl2583_set_pm_runtime_busy(chip, false);
799 if (ret < 0)
800 return ret;
801
Brian Masneyb3d94152016-10-28 06:00:17 -0400802 return ret;
803}
804
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100805static const struct iio_info tsl2583_info = {
806 .attrs = &tsl2583_attribute_group,
807 .driver_module = THIS_MODULE,
Brian Masneyb3d94152016-10-28 06:00:17 -0400808 .read_raw = tsl2583_read_raw,
809 .write_raw = tsl2583_write_raw,
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100810};
811
Brian Masney686c5922016-11-12 13:19:23 -0500812static int tsl2583_probe(struct i2c_client *clientp,
813 const struct i2c_device_id *idp)
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100814{
Brian Masney6bd0cb22016-11-03 08:56:13 -0400815 int ret;
Jonathan Camerone05e5f22011-08-12 17:08:52 +0100816 struct tsl2583_chip *chip;
817 struct iio_dev *indio_dev;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100818
819 if (!i2c_check_functionality(clientp->adapter,
Eva Rachel Retuyaca52c882016-02-18 15:35:36 +0800820 I2C_FUNC_SMBUS_BYTE_DATA)) {
Brian Masney24ad4302016-11-12 13:19:22 -0500821 dev_err(&clientp->dev, "%s: i2c smbus byte data functionality is unsupported\n",
822 __func__);
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100823 return -EOPNOTSUPP;
824 }
825
Sachin Kamat664716ae2013-09-05 10:29:00 +0100826 indio_dev = devm_iio_device_alloc(&clientp->dev, sizeof(*chip));
827 if (!indio_dev)
828 return -ENOMEM;
Brian Masney184f9162016-11-12 13:19:26 -0500829
Jonathan Camerone05e5f22011-08-12 17:08:52 +0100830 chip = iio_priv(indio_dev);
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100831 chip->client = clientp;
Jonathan Camerone05e5f22011-08-12 17:08:52 +0100832 i2c_set_clientdata(clientp, indio_dev);
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100833
834 mutex_init(&chip->als_mutex);
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100835
Brian Masney6bd0cb22016-11-03 08:56:13 -0400836 ret = i2c_smbus_read_byte_data(clientp,
Brian Masney686c5922016-11-12 13:19:23 -0500837 TSL2583_CMD_REG | TSL2583_CHIPID);
Brian Masney6bd0cb22016-11-03 08:56:13 -0400838 if (ret < 0) {
839 dev_err(&clientp->dev,
Brian Masney24ad4302016-11-12 13:19:22 -0500840 "%s: failed to read the chip ID register\n", __func__);
Brian Masney6bd0cb22016-11-03 08:56:13 -0400841 return ret;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100842 }
843
Brian Masney6bd0cb22016-11-03 08:56:13 -0400844 if ((ret & TSL2583_CHIP_ID_MASK) != TSL2583_CHIP_ID) {
Brian Masney24ad4302016-11-12 13:19:22 -0500845 dev_err(&clientp->dev, "%s: received an unknown chip ID %x\n",
846 __func__, ret);
Sachin Kamat664716ae2013-09-05 10:29:00 +0100847 return -EINVAL;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100848 }
849
Jonathan Camerone05e5f22011-08-12 17:08:52 +0100850 indio_dev->info = &tsl2583_info;
Brian Masneyb3d94152016-10-28 06:00:17 -0400851 indio_dev->channels = tsl2583_channels;
852 indio_dev->num_channels = ARRAY_SIZE(tsl2583_channels);
Jonathan Camerone05e5f22011-08-12 17:08:52 +0100853 indio_dev->dev.parent = &clientp->dev;
854 indio_dev->modes = INDIO_DIRECT_MODE;
855 indio_dev->name = chip->client->name;
Brian Masney184f9162016-11-12 13:19:26 -0500856
Brian Masney371894f2017-04-25 04:06:33 -0400857 pm_runtime_enable(&clientp->dev);
858 pm_runtime_set_autosuspend_delay(&clientp->dev,
859 TSL2583_POWER_OFF_DELAY_MS);
860 pm_runtime_use_autosuspend(&clientp->dev);
861
Ioana Ciornei86727e32015-10-02 13:37:49 +0300862 ret = devm_iio_device_register(indio_dev->dev.parent, indio_dev);
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100863 if (ret) {
Brian Masney24ad4302016-11-12 13:19:22 -0500864 dev_err(&clientp->dev, "%s: iio registration failed\n",
865 __func__);
Sachin Kamat664716ae2013-09-05 10:29:00 +0100866 return ret;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100867 }
868
869 /* Load up the V2 defaults (these are hard coded defaults for now) */
Brian Masney686c5922016-11-12 13:19:23 -0500870 tsl2583_defaults(chip);
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100871
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100872 dev_info(&clientp->dev, "Light sensor found.\n");
Brian Masney184f9162016-11-12 13:19:26 -0500873
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100874 return 0;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100875}
876
Brian Masney371894f2017-04-25 04:06:33 -0400877static int tsl2583_remove(struct i2c_client *client)
878{
879 struct iio_dev *indio_dev = i2c_get_clientdata(client);
880 struct tsl2583_chip *chip = iio_priv(indio_dev);
881
882 iio_device_unregister(indio_dev);
883
884 pm_runtime_disable(&client->dev);
885 pm_runtime_set_suspended(&client->dev);
886 pm_runtime_put_noidle(&client->dev);
887
888 return tsl2583_set_power_state(chip, TSL2583_CNTL_PWR_OFF);
889}
890
Brian Masney686c5922016-11-12 13:19:23 -0500891static int __maybe_unused tsl2583_suspend(struct device *dev)
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100892{
Lars-Peter Clausen01788c52012-02-20 19:37:05 +0100893 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
Jonathan Camerone05e5f22011-08-12 17:08:52 +0100894 struct tsl2583_chip *chip = iio_priv(indio_dev);
Brian Masney0859fdd2016-11-12 13:19:39 -0500895 int ret;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100896
897 mutex_lock(&chip->als_mutex);
898
Brian Masney686c5922016-11-12 13:19:23 -0500899 ret = tsl2583_set_power_state(chip, TSL2583_CNTL_PWR_OFF);
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100900
901 mutex_unlock(&chip->als_mutex);
Brian Masney184f9162016-11-12 13:19:26 -0500902
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100903 return ret;
904}
905
Brian Masney686c5922016-11-12 13:19:23 -0500906static int __maybe_unused tsl2583_resume(struct device *dev)
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100907{
Lars-Peter Clausen01788c52012-02-20 19:37:05 +0100908 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
Jonathan Camerone05e5f22011-08-12 17:08:52 +0100909 struct tsl2583_chip *chip = iio_priv(indio_dev);
Brian Masney0859fdd2016-11-12 13:19:39 -0500910 int ret;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100911
912 mutex_lock(&chip->als_mutex);
913
Brian Masney2ff8a352016-11-12 13:19:18 -0500914 ret = tsl2583_chip_init_and_power_on(indio_dev);
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100915
916 mutex_unlock(&chip->als_mutex);
Brian Masney184f9162016-11-12 13:19:26 -0500917
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100918 return ret;
919}
920
Brian Masney371894f2017-04-25 04:06:33 -0400921static const struct dev_pm_ops tsl2583_pm_ops = {
922 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
923 pm_runtime_force_resume)
924 SET_RUNTIME_PM_OPS(tsl2583_suspend, tsl2583_resume, NULL)
925};
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100926
Arvind Yadav3282fa32017-08-20 00:17:38 +0530927static const struct i2c_device_id tsl2583_idtable[] = {
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100928 { "tsl2580", 0 },
929 { "tsl2581", 1 },
930 { "tsl2583", 2 },
931 {}
932};
Brian Masney686c5922016-11-12 13:19:23 -0500933MODULE_DEVICE_TABLE(i2c, tsl2583_idtable);
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100934
Brian Masney686c5922016-11-12 13:19:23 -0500935static const struct of_device_id tsl2583_of_match[] = {
Brian Masneya3b6b4b2016-10-28 06:00:12 -0400936 { .compatible = "amstaos,tsl2580", },
937 { .compatible = "amstaos,tsl2581", },
938 { .compatible = "amstaos,tsl2583", },
939 { },
940};
Brian Masney686c5922016-11-12 13:19:23 -0500941MODULE_DEVICE_TABLE(of, tsl2583_of_match);
Brian Masneya3b6b4b2016-10-28 06:00:12 -0400942
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100943/* Driver definition */
Brian Masney686c5922016-11-12 13:19:23 -0500944static struct i2c_driver tsl2583_driver = {
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100945 .driver = {
946 .name = "tsl2583",
Brian Masney686c5922016-11-12 13:19:23 -0500947 .pm = &tsl2583_pm_ops,
948 .of_match_table = tsl2583_of_match,
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100949 },
Brian Masney686c5922016-11-12 13:19:23 -0500950 .id_table = tsl2583_idtable,
951 .probe = tsl2583_probe,
Brian Masney371894f2017-04-25 04:06:33 -0400952 .remove = tsl2583_remove,
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100953};
Brian Masney686c5922016-11-12 13:19:23 -0500954module_i2c_driver(tsl2583_driver);
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100955
Brian Masneyc45a2262016-11-12 13:19:40 -0500956MODULE_AUTHOR("J. August Brenner <jbrenner@taosinc.com>");
957MODULE_AUTHOR("Brian Masney <masneyb@onstation.org>");
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100958MODULE_DESCRIPTION("TAOS tsl2583 ambient light sensor driver");
959MODULE_LICENSE("GPL");