tsl2583.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Device driver for monitoring ambient light intensity (lux)
  4. * within the TAOS tsl258x family of devices (tsl2580, tsl2581, tsl2583).
  5. *
  6. * Copyright (c) 2011, TAOS Corporation.
  7. * Copyright (c) 2016-2017 Brian Masney <[email protected]>
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/i2c.h>
  11. #include <linux/errno.h>
  12. #include <linux/delay.h>
  13. #include <linux/string.h>
  14. #include <linux/mutex.h>
  15. #include <linux/unistd.h>
  16. #include <linux/slab.h>
  17. #include <linux/module.h>
  18. #include <linux/iio/iio.h>
  19. #include <linux/iio/sysfs.h>
  20. #include <linux/pm_runtime.h>
  21. /* Device Registers and Masks */
  22. #define TSL2583_CNTRL 0x00
  23. #define TSL2583_ALS_TIME 0X01
  24. #define TSL2583_INTERRUPT 0x02
  25. #define TSL2583_GAIN 0x07
  26. #define TSL2583_REVID 0x11
  27. #define TSL2583_CHIPID 0x12
  28. #define TSL2583_ALS_CHAN0LO 0x14
  29. #define TSL2583_ALS_CHAN0HI 0x15
  30. #define TSL2583_ALS_CHAN1LO 0x16
  31. #define TSL2583_ALS_CHAN1HI 0x17
  32. #define TSL2583_TMR_LO 0x18
  33. #define TSL2583_TMR_HI 0x19
  34. /* tsl2583 cmd reg masks */
  35. #define TSL2583_CMD_REG 0x80
  36. #define TSL2583_CMD_SPL_FN 0x60
  37. #define TSL2583_CMD_ALS_INT_CLR 0x01
  38. /* tsl2583 cntrl reg masks */
  39. #define TSL2583_CNTL_ADC_ENBL 0x02
  40. #define TSL2583_CNTL_PWR_OFF 0x00
  41. #define TSL2583_CNTL_PWR_ON 0x01
  42. /* tsl2583 status reg masks */
  43. #define TSL2583_STA_ADC_VALID 0x01
  44. #define TSL2583_STA_ADC_INTR 0x10
  45. /* Lux calculation constants */
  46. #define TSL2583_LUX_CALC_OVER_FLOW 65535
  47. #define TSL2583_INTERRUPT_DISABLED 0x00
  48. #define TSL2583_CHIP_ID 0x90
  49. #define TSL2583_CHIP_ID_MASK 0xf0
  50. #define TSL2583_POWER_OFF_DELAY_MS 2000
  51. /* Per-device data */
  52. struct tsl2583_als_info {
  53. u16 als_ch0;
  54. u16 als_ch1;
  55. u16 lux;
  56. };
  57. struct tsl2583_lux {
  58. unsigned int ratio;
  59. unsigned int ch0;
  60. unsigned int ch1;
  61. };
  62. static const struct tsl2583_lux tsl2583_default_lux[] = {
  63. { 9830, 8520, 15729 },
  64. { 12452, 10807, 23344 },
  65. { 14746, 6383, 11705 },
  66. { 17695, 4063, 6554 },
  67. { 0, 0, 0 } /* Termination segment */
  68. };
  69. #define TSL2583_MAX_LUX_TABLE_ENTRIES 11
  70. struct tsl2583_settings {
  71. int als_time;
  72. int als_gain;
  73. int als_gain_trim;
  74. int als_cal_target;
  75. /*
  76. * This structure is intentionally large to accommodate updates via
  77. * sysfs. Sized to 11 = max 10 segments + 1 termination segment.
  78. * Assumption is that one and only one type of glass used.
  79. */
  80. struct tsl2583_lux als_device_lux[TSL2583_MAX_LUX_TABLE_ENTRIES];
  81. };
  82. struct tsl2583_chip {
  83. struct mutex als_mutex;
  84. struct i2c_client *client;
  85. struct tsl2583_als_info als_cur_info;
  86. struct tsl2583_settings als_settings;
  87. int als_time_scale;
  88. int als_saturation;
  89. };
  90. struct gainadj {
  91. s16 ch0;
  92. s16 ch1;
  93. s16 mean;
  94. };
  95. /* Index = (0 - 3) Used to validate the gain selection index */
  96. static const struct gainadj gainadj[] = {
  97. { 1, 1, 1 },
  98. { 8, 8, 8 },
  99. { 16, 16, 16 },
  100. { 107, 115, 111 }
  101. };
  102. /*
  103. * Provides initial operational parameter defaults.
  104. * These defaults may be changed through the device's sysfs files.
  105. */
  106. static void tsl2583_defaults(struct tsl2583_chip *chip)
  107. {
  108. /*
  109. * The integration time must be a multiple of 50ms and within the
  110. * range [50, 600] ms.
  111. */
  112. chip->als_settings.als_time = 100;
  113. /*
  114. * This is an index into the gainadj table. Assume clear glass as the
  115. * default.
  116. */
  117. chip->als_settings.als_gain = 0;
  118. /* Default gain trim to account for aperture effects */
  119. chip->als_settings.als_gain_trim = 1000;
  120. /* Known external ALS reading used for calibration */
  121. chip->als_settings.als_cal_target = 130;
  122. /* Default lux table. */
  123. memcpy(chip->als_settings.als_device_lux, tsl2583_default_lux,
  124. sizeof(tsl2583_default_lux));
  125. }
  126. /*
  127. * Reads and calculates current lux value.
  128. * The raw ch0 and ch1 values of the ambient light sensed in the last
  129. * integration cycle are read from the device.
  130. * Time scale factor array values are adjusted based on the integration time.
  131. * The raw values are multiplied by a scale factor, and device gain is obtained
  132. * using gain index. Limit checks are done next, then the ratio of a multiple
  133. * of ch1 value, to the ch0 value, is calculated. The array als_device_lux[]
  134. * declared above is then scanned to find the first ratio value that is just
  135. * above the ratio we just calculated. The ch0 and ch1 multiplier constants in
  136. * the array are then used along with the time scale factor array values, to
  137. * calculate the lux.
  138. */
  139. static int tsl2583_get_lux(struct iio_dev *indio_dev)
  140. {
  141. u16 ch0, ch1; /* separated ch0/ch1 data from device */
  142. u32 lux; /* raw lux calculated from device data */
  143. u64 lux64;
  144. u32 ratio;
  145. u8 buf[5];
  146. struct tsl2583_lux *p;
  147. struct tsl2583_chip *chip = iio_priv(indio_dev);
  148. int i, ret;
  149. ret = i2c_smbus_read_byte_data(chip->client, TSL2583_CMD_REG);
  150. if (ret < 0) {
  151. dev_err(&chip->client->dev, "%s: failed to read CMD_REG register\n",
  152. __func__);
  153. goto done;
  154. }
  155. /* is data new & valid */
  156. if (!(ret & TSL2583_STA_ADC_INTR)) {
  157. dev_err(&chip->client->dev, "%s: data not valid; returning last value\n",
  158. __func__);
  159. ret = chip->als_cur_info.lux; /* return LAST VALUE */
  160. goto done;
  161. }
  162. for (i = 0; i < 4; i++) {
  163. int reg = TSL2583_CMD_REG | (TSL2583_ALS_CHAN0LO + i);
  164. ret = i2c_smbus_read_byte_data(chip->client, reg);
  165. if (ret < 0) {
  166. dev_err(&chip->client->dev, "%s: failed to read register %x\n",
  167. __func__, reg);
  168. goto done;
  169. }
  170. buf[i] = ret;
  171. }
  172. /*
  173. * Clear the pending interrupt status bit on the chip to allow the next
  174. * integration cycle to start. This has to be done even though this
  175. * driver currently does not support interrupts.
  176. */
  177. ret = i2c_smbus_write_byte(chip->client,
  178. (TSL2583_CMD_REG | TSL2583_CMD_SPL_FN |
  179. TSL2583_CMD_ALS_INT_CLR));
  180. if (ret < 0) {
  181. dev_err(&chip->client->dev, "%s: failed to clear the interrupt bit\n",
  182. __func__);
  183. goto done; /* have no data, so return failure */
  184. }
  185. /* extract ALS/lux data */
  186. ch0 = le16_to_cpup((const __le16 *)&buf[0]);
  187. ch1 = le16_to_cpup((const __le16 *)&buf[2]);
  188. chip->als_cur_info.als_ch0 = ch0;
  189. chip->als_cur_info.als_ch1 = ch1;
  190. if ((ch0 >= chip->als_saturation) || (ch1 >= chip->als_saturation))
  191. goto return_max;
  192. if (!ch0) {
  193. /*
  194. * The sensor appears to be in total darkness so set the
  195. * calculated lux to 0 and return early to avoid a division by
  196. * zero below when calculating the ratio.
  197. */
  198. ret = 0;
  199. chip->als_cur_info.lux = 0;
  200. goto done;
  201. }
  202. /* calculate ratio */
  203. ratio = (ch1 << 15) / ch0;
  204. /* convert to unscaled lux using the pointer to the table */
  205. for (p = (struct tsl2583_lux *)chip->als_settings.als_device_lux;
  206. p->ratio != 0 && p->ratio < ratio; p++)
  207. ;
  208. if (p->ratio == 0) {
  209. lux = 0;
  210. } else {
  211. u32 ch0lux, ch1lux;
  212. ch0lux = ((ch0 * p->ch0) +
  213. (gainadj[chip->als_settings.als_gain].ch0 >> 1))
  214. / gainadj[chip->als_settings.als_gain].ch0;
  215. ch1lux = ((ch1 * p->ch1) +
  216. (gainadj[chip->als_settings.als_gain].ch1 >> 1))
  217. / gainadj[chip->als_settings.als_gain].ch1;
  218. /* note: lux is 31 bit max at this point */
  219. if (ch1lux > ch0lux) {
  220. dev_dbg(&chip->client->dev, "%s: No Data - Returning 0\n",
  221. __func__);
  222. ret = 0;
  223. chip->als_cur_info.lux = 0;
  224. goto done;
  225. }
  226. lux = ch0lux - ch1lux;
  227. }
  228. /* adjust for active time scale */
  229. if (chip->als_time_scale == 0)
  230. lux = 0;
  231. else
  232. lux = (lux + (chip->als_time_scale >> 1)) /
  233. chip->als_time_scale;
  234. /*
  235. * Adjust for active gain scale.
  236. * The tsl2583_default_lux tables above have a factor of 8192 built in,
  237. * so we need to shift right.
  238. * User-specified gain provides a multiplier.
  239. * Apply user-specified gain before shifting right to retain precision.
  240. * Use 64 bits to avoid overflow on multiplication.
  241. * Then go back to 32 bits before division to avoid using div_u64().
  242. */
  243. lux64 = lux;
  244. lux64 = lux64 * chip->als_settings.als_gain_trim;
  245. lux64 >>= 13;
  246. lux = lux64;
  247. lux = DIV_ROUND_CLOSEST(lux, 1000);
  248. if (lux > TSL2583_LUX_CALC_OVER_FLOW) { /* check for overflow */
  249. return_max:
  250. lux = TSL2583_LUX_CALC_OVER_FLOW;
  251. }
  252. /* Update the structure with the latest VALID lux. */
  253. chip->als_cur_info.lux = lux;
  254. ret = lux;
  255. done:
  256. return ret;
  257. }
  258. /*
  259. * Obtain single reading and calculate the als_gain_trim (later used
  260. * to derive actual lux).
  261. * Return updated gain_trim value.
  262. */
  263. static int tsl2583_als_calibrate(struct iio_dev *indio_dev)
  264. {
  265. struct tsl2583_chip *chip = iio_priv(indio_dev);
  266. unsigned int gain_trim_val;
  267. int ret;
  268. int lux_val;
  269. ret = i2c_smbus_read_byte_data(chip->client,
  270. TSL2583_CMD_REG | TSL2583_CNTRL);
  271. if (ret < 0) {
  272. dev_err(&chip->client->dev,
  273. "%s: failed to read from the CNTRL register\n",
  274. __func__);
  275. return ret;
  276. }
  277. if ((ret & (TSL2583_CNTL_ADC_ENBL | TSL2583_CNTL_PWR_ON))
  278. != (TSL2583_CNTL_ADC_ENBL | TSL2583_CNTL_PWR_ON)) {
  279. dev_err(&chip->client->dev,
  280. "%s: Device is not powered on and/or ADC is not enabled\n",
  281. __func__);
  282. return -EINVAL;
  283. } else if ((ret & TSL2583_STA_ADC_VALID) != TSL2583_STA_ADC_VALID) {
  284. dev_err(&chip->client->dev,
  285. "%s: The two ADC channels have not completed an integration cycle\n",
  286. __func__);
  287. return -ENODATA;
  288. }
  289. lux_val = tsl2583_get_lux(indio_dev);
  290. if (lux_val < 0) {
  291. dev_err(&chip->client->dev, "%s: failed to get lux\n",
  292. __func__);
  293. return lux_val;
  294. }
  295. /* Avoid division by zero of lux_value later on */
  296. if (lux_val == 0) {
  297. dev_err(&chip->client->dev,
  298. "%s: lux_val of 0 will produce out of range trim_value\n",
  299. __func__);
  300. return -ENODATA;
  301. }
  302. gain_trim_val = (unsigned int)(((chip->als_settings.als_cal_target)
  303. * chip->als_settings.als_gain_trim) / lux_val);
  304. if ((gain_trim_val < 250) || (gain_trim_val > 4000)) {
  305. dev_err(&chip->client->dev,
  306. "%s: trim_val of %d is not within the range [250, 4000]\n",
  307. __func__, gain_trim_val);
  308. return -ENODATA;
  309. }
  310. chip->als_settings.als_gain_trim = (int)gain_trim_val;
  311. return 0;
  312. }
  313. static int tsl2583_set_als_time(struct tsl2583_chip *chip)
  314. {
  315. int als_count, als_time, ret;
  316. u8 val;
  317. /* determine als integration register */
  318. als_count = DIV_ROUND_CLOSEST(chip->als_settings.als_time * 100, 270);
  319. if (!als_count)
  320. als_count = 1; /* ensure at least one cycle */
  321. /* convert back to time (encompasses overrides) */
  322. als_time = DIV_ROUND_CLOSEST(als_count * 27, 10);
  323. val = 256 - als_count;
  324. ret = i2c_smbus_write_byte_data(chip->client,
  325. TSL2583_CMD_REG | TSL2583_ALS_TIME,
  326. val);
  327. if (ret < 0) {
  328. dev_err(&chip->client->dev, "%s: failed to set the als time to %d\n",
  329. __func__, val);
  330. return ret;
  331. }
  332. /* set chip struct re scaling and saturation */
  333. chip->als_saturation = als_count * 922; /* 90% of full scale */
  334. chip->als_time_scale = DIV_ROUND_CLOSEST(als_time, 50);
  335. return ret;
  336. }
  337. static int tsl2583_set_als_gain(struct tsl2583_chip *chip)
  338. {
  339. int ret;
  340. /* Set the gain based on als_settings struct */
  341. ret = i2c_smbus_write_byte_data(chip->client,
  342. TSL2583_CMD_REG | TSL2583_GAIN,
  343. chip->als_settings.als_gain);
  344. if (ret < 0)
  345. dev_err(&chip->client->dev,
  346. "%s: failed to set the gain to %d\n", __func__,
  347. chip->als_settings.als_gain);
  348. return ret;
  349. }
  350. static int tsl2583_set_power_state(struct tsl2583_chip *chip, u8 state)
  351. {
  352. int ret;
  353. ret = i2c_smbus_write_byte_data(chip->client,
  354. TSL2583_CMD_REG | TSL2583_CNTRL, state);
  355. if (ret < 0)
  356. dev_err(&chip->client->dev,
  357. "%s: failed to set the power state to %d\n", __func__,
  358. state);
  359. return ret;
  360. }
  361. /*
  362. * Turn the device on.
  363. * Configuration must be set before calling this function.
  364. */
  365. static int tsl2583_chip_init_and_power_on(struct iio_dev *indio_dev)
  366. {
  367. struct tsl2583_chip *chip = iio_priv(indio_dev);
  368. int ret;
  369. /* Power on the device; ADC off. */
  370. ret = tsl2583_set_power_state(chip, TSL2583_CNTL_PWR_ON);
  371. if (ret < 0)
  372. return ret;
  373. ret = i2c_smbus_write_byte_data(chip->client,
  374. TSL2583_CMD_REG | TSL2583_INTERRUPT,
  375. TSL2583_INTERRUPT_DISABLED);
  376. if (ret < 0) {
  377. dev_err(&chip->client->dev,
  378. "%s: failed to disable interrupts\n", __func__);
  379. return ret;
  380. }
  381. ret = tsl2583_set_als_time(chip);
  382. if (ret < 0)
  383. return ret;
  384. ret = tsl2583_set_als_gain(chip);
  385. if (ret < 0)
  386. return ret;
  387. usleep_range(3000, 3500);
  388. ret = tsl2583_set_power_state(chip, TSL2583_CNTL_PWR_ON |
  389. TSL2583_CNTL_ADC_ENBL);
  390. if (ret < 0)
  391. return ret;
  392. return ret;
  393. }
  394. /* Sysfs Interface Functions */
  395. static ssize_t in_illuminance_input_target_show(struct device *dev,
  396. struct device_attribute *attr,
  397. char *buf)
  398. {
  399. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  400. struct tsl2583_chip *chip = iio_priv(indio_dev);
  401. int ret;
  402. mutex_lock(&chip->als_mutex);
  403. ret = sprintf(buf, "%d\n", chip->als_settings.als_cal_target);
  404. mutex_unlock(&chip->als_mutex);
  405. return ret;
  406. }
  407. static ssize_t in_illuminance_input_target_store(struct device *dev,
  408. struct device_attribute *attr,
  409. const char *buf, size_t len)
  410. {
  411. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  412. struct tsl2583_chip *chip = iio_priv(indio_dev);
  413. int value;
  414. if (kstrtoint(buf, 0, &value) || !value)
  415. return -EINVAL;
  416. mutex_lock(&chip->als_mutex);
  417. chip->als_settings.als_cal_target = value;
  418. mutex_unlock(&chip->als_mutex);
  419. return len;
  420. }
  421. static ssize_t in_illuminance_calibrate_store(struct device *dev,
  422. struct device_attribute *attr,
  423. const char *buf, size_t len)
  424. {
  425. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  426. struct tsl2583_chip *chip = iio_priv(indio_dev);
  427. int value, ret;
  428. if (kstrtoint(buf, 0, &value) || value != 1)
  429. return -EINVAL;
  430. mutex_lock(&chip->als_mutex);
  431. ret = tsl2583_als_calibrate(indio_dev);
  432. if (ret < 0)
  433. goto done;
  434. ret = len;
  435. done:
  436. mutex_unlock(&chip->als_mutex);
  437. return ret;
  438. }
  439. static ssize_t in_illuminance_lux_table_show(struct device *dev,
  440. struct device_attribute *attr,
  441. char *buf)
  442. {
  443. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  444. struct tsl2583_chip *chip = iio_priv(indio_dev);
  445. unsigned int i;
  446. int offset = 0;
  447. for (i = 0; i < ARRAY_SIZE(chip->als_settings.als_device_lux); i++) {
  448. offset += sprintf(buf + offset, "%u,%u,%u,",
  449. chip->als_settings.als_device_lux[i].ratio,
  450. chip->als_settings.als_device_lux[i].ch0,
  451. chip->als_settings.als_device_lux[i].ch1);
  452. if (chip->als_settings.als_device_lux[i].ratio == 0) {
  453. /*
  454. * We just printed the first "0" entry.
  455. * Now get rid of the extra "," and break.
  456. */
  457. offset--;
  458. break;
  459. }
  460. }
  461. offset += sprintf(buf + offset, "\n");
  462. return offset;
  463. }
  464. static ssize_t in_illuminance_lux_table_store(struct device *dev,
  465. struct device_attribute *attr,
  466. const char *buf, size_t len)
  467. {
  468. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  469. struct tsl2583_chip *chip = iio_priv(indio_dev);
  470. const unsigned int max_ints = TSL2583_MAX_LUX_TABLE_ENTRIES * 3;
  471. int value[TSL2583_MAX_LUX_TABLE_ENTRIES * 3 + 1];
  472. int ret = -EINVAL;
  473. unsigned int n;
  474. mutex_lock(&chip->als_mutex);
  475. get_options(buf, ARRAY_SIZE(value), value);
  476. /*
  477. * We now have an array of ints starting at value[1], and
  478. * enumerated by value[0].
  479. * We expect each group of three ints is one table entry,
  480. * and the last table entry is all 0.
  481. */
  482. n = value[0];
  483. if ((n % 3) || n < 6 || n > max_ints) {
  484. dev_err(dev,
  485. "%s: The number of entries in the lux table must be a multiple of 3 and within the range [6, %d]\n",
  486. __func__, max_ints);
  487. goto done;
  488. }
  489. if ((value[n - 2] | value[n - 1] | value[n]) != 0) {
  490. dev_err(dev, "%s: The last 3 entries in the lux table must be zeros.\n",
  491. __func__);
  492. goto done;
  493. }
  494. memcpy(chip->als_settings.als_device_lux, &value[1],
  495. value[0] * sizeof(value[1]));
  496. ret = len;
  497. done:
  498. mutex_unlock(&chip->als_mutex);
  499. return ret;
  500. }
  501. static IIO_CONST_ATTR(in_illuminance_calibscale_available, "1 8 16 111");
  502. static IIO_CONST_ATTR(in_illuminance_integration_time_available,
  503. "0.050 0.100 0.150 0.200 0.250 0.300 0.350 0.400 0.450 0.500 0.550 0.600 0.650");
  504. static IIO_DEVICE_ATTR_RW(in_illuminance_input_target, 0);
  505. static IIO_DEVICE_ATTR_WO(in_illuminance_calibrate, 0);
  506. static IIO_DEVICE_ATTR_RW(in_illuminance_lux_table, 0);
  507. static struct attribute *sysfs_attrs_ctrl[] = {
  508. &iio_const_attr_in_illuminance_calibscale_available.dev_attr.attr,
  509. &iio_const_attr_in_illuminance_integration_time_available.dev_attr.attr,
  510. &iio_dev_attr_in_illuminance_input_target.dev_attr.attr,
  511. &iio_dev_attr_in_illuminance_calibrate.dev_attr.attr,
  512. &iio_dev_attr_in_illuminance_lux_table.dev_attr.attr,
  513. NULL
  514. };
  515. static const struct attribute_group tsl2583_attribute_group = {
  516. .attrs = sysfs_attrs_ctrl,
  517. };
  518. static const struct iio_chan_spec tsl2583_channels[] = {
  519. {
  520. .type = IIO_LIGHT,
  521. .modified = 1,
  522. .channel2 = IIO_MOD_LIGHT_IR,
  523. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  524. },
  525. {
  526. .type = IIO_LIGHT,
  527. .modified = 1,
  528. .channel2 = IIO_MOD_LIGHT_BOTH,
  529. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  530. },
  531. {
  532. .type = IIO_LIGHT,
  533. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
  534. BIT(IIO_CHAN_INFO_CALIBBIAS) |
  535. BIT(IIO_CHAN_INFO_CALIBSCALE) |
  536. BIT(IIO_CHAN_INFO_INT_TIME),
  537. },
  538. };
  539. static int tsl2583_set_pm_runtime_busy(struct tsl2583_chip *chip, bool on)
  540. {
  541. int ret;
  542. if (on) {
  543. ret = pm_runtime_resume_and_get(&chip->client->dev);
  544. } else {
  545. pm_runtime_mark_last_busy(&chip->client->dev);
  546. ret = pm_runtime_put_autosuspend(&chip->client->dev);
  547. }
  548. return ret;
  549. }
  550. static int tsl2583_read_raw(struct iio_dev *indio_dev,
  551. struct iio_chan_spec const *chan,
  552. int *val, int *val2, long mask)
  553. {
  554. struct tsl2583_chip *chip = iio_priv(indio_dev);
  555. int ret, pm_ret;
  556. ret = tsl2583_set_pm_runtime_busy(chip, true);
  557. if (ret < 0)
  558. return ret;
  559. mutex_lock(&chip->als_mutex);
  560. ret = -EINVAL;
  561. switch (mask) {
  562. case IIO_CHAN_INFO_RAW:
  563. if (chan->type == IIO_LIGHT) {
  564. ret = tsl2583_get_lux(indio_dev);
  565. if (ret < 0)
  566. goto read_done;
  567. /*
  568. * From page 20 of the TSL2581, TSL2583 data
  569. * sheet (TAOS134 − MARCH 2011):
  570. *
  571. * One of the photodiodes (channel 0) is
  572. * sensitive to both visible and infrared light,
  573. * while the second photodiode (channel 1) is
  574. * sensitive primarily to infrared light.
  575. */
  576. if (chan->channel2 == IIO_MOD_LIGHT_BOTH)
  577. *val = chip->als_cur_info.als_ch0;
  578. else
  579. *val = chip->als_cur_info.als_ch1;
  580. ret = IIO_VAL_INT;
  581. }
  582. break;
  583. case IIO_CHAN_INFO_PROCESSED:
  584. if (chan->type == IIO_LIGHT) {
  585. ret = tsl2583_get_lux(indio_dev);
  586. if (ret < 0)
  587. goto read_done;
  588. *val = ret;
  589. ret = IIO_VAL_INT;
  590. }
  591. break;
  592. case IIO_CHAN_INFO_CALIBBIAS:
  593. if (chan->type == IIO_LIGHT) {
  594. *val = chip->als_settings.als_gain_trim;
  595. ret = IIO_VAL_INT;
  596. }
  597. break;
  598. case IIO_CHAN_INFO_CALIBSCALE:
  599. if (chan->type == IIO_LIGHT) {
  600. *val = gainadj[chip->als_settings.als_gain].mean;
  601. ret = IIO_VAL_INT;
  602. }
  603. break;
  604. case IIO_CHAN_INFO_INT_TIME:
  605. if (chan->type == IIO_LIGHT) {
  606. *val = 0;
  607. *val2 = chip->als_settings.als_time;
  608. ret = IIO_VAL_INT_PLUS_MICRO;
  609. }
  610. break;
  611. default:
  612. break;
  613. }
  614. read_done:
  615. mutex_unlock(&chip->als_mutex);
  616. if (ret < 0) {
  617. tsl2583_set_pm_runtime_busy(chip, false);
  618. return ret;
  619. }
  620. /*
  621. * Preserve the ret variable if the call to
  622. * tsl2583_set_pm_runtime_busy() is successful so the reading
  623. * (if applicable) is returned to user space.
  624. */
  625. pm_ret = tsl2583_set_pm_runtime_busy(chip, false);
  626. if (pm_ret < 0)
  627. return pm_ret;
  628. return ret;
  629. }
  630. static int tsl2583_write_raw(struct iio_dev *indio_dev,
  631. struct iio_chan_spec const *chan,
  632. int val, int val2, long mask)
  633. {
  634. struct tsl2583_chip *chip = iio_priv(indio_dev);
  635. int ret;
  636. ret = tsl2583_set_pm_runtime_busy(chip, true);
  637. if (ret < 0)
  638. return ret;
  639. mutex_lock(&chip->als_mutex);
  640. ret = -EINVAL;
  641. switch (mask) {
  642. case IIO_CHAN_INFO_CALIBBIAS:
  643. if (chan->type == IIO_LIGHT) {
  644. chip->als_settings.als_gain_trim = val;
  645. ret = 0;
  646. }
  647. break;
  648. case IIO_CHAN_INFO_CALIBSCALE:
  649. if (chan->type == IIO_LIGHT) {
  650. unsigned int i;
  651. for (i = 0; i < ARRAY_SIZE(gainadj); i++) {
  652. if (gainadj[i].mean == val) {
  653. chip->als_settings.als_gain = i;
  654. ret = tsl2583_set_als_gain(chip);
  655. break;
  656. }
  657. }
  658. }
  659. break;
  660. case IIO_CHAN_INFO_INT_TIME:
  661. if (chan->type == IIO_LIGHT && !val && val2 >= 50 &&
  662. val2 <= 650 && !(val2 % 50)) {
  663. chip->als_settings.als_time = val2;
  664. ret = tsl2583_set_als_time(chip);
  665. }
  666. break;
  667. default:
  668. break;
  669. }
  670. mutex_unlock(&chip->als_mutex);
  671. if (ret < 0) {
  672. tsl2583_set_pm_runtime_busy(chip, false);
  673. return ret;
  674. }
  675. ret = tsl2583_set_pm_runtime_busy(chip, false);
  676. if (ret < 0)
  677. return ret;
  678. return ret;
  679. }
  680. static const struct iio_info tsl2583_info = {
  681. .attrs = &tsl2583_attribute_group,
  682. .read_raw = tsl2583_read_raw,
  683. .write_raw = tsl2583_write_raw,
  684. };
  685. static int tsl2583_probe(struct i2c_client *clientp,
  686. const struct i2c_device_id *idp)
  687. {
  688. int ret;
  689. struct tsl2583_chip *chip;
  690. struct iio_dev *indio_dev;
  691. if (!i2c_check_functionality(clientp->adapter,
  692. I2C_FUNC_SMBUS_BYTE_DATA)) {
  693. dev_err(&clientp->dev, "%s: i2c smbus byte data functionality is unsupported\n",
  694. __func__);
  695. return -EOPNOTSUPP;
  696. }
  697. indio_dev = devm_iio_device_alloc(&clientp->dev, sizeof(*chip));
  698. if (!indio_dev)
  699. return -ENOMEM;
  700. chip = iio_priv(indio_dev);
  701. chip->client = clientp;
  702. i2c_set_clientdata(clientp, indio_dev);
  703. mutex_init(&chip->als_mutex);
  704. ret = i2c_smbus_read_byte_data(clientp,
  705. TSL2583_CMD_REG | TSL2583_CHIPID);
  706. if (ret < 0) {
  707. dev_err(&clientp->dev,
  708. "%s: failed to read the chip ID register\n", __func__);
  709. return ret;
  710. }
  711. if ((ret & TSL2583_CHIP_ID_MASK) != TSL2583_CHIP_ID) {
  712. dev_err(&clientp->dev, "%s: received an unknown chip ID %x\n",
  713. __func__, ret);
  714. return -EINVAL;
  715. }
  716. indio_dev->info = &tsl2583_info;
  717. indio_dev->channels = tsl2583_channels;
  718. indio_dev->num_channels = ARRAY_SIZE(tsl2583_channels);
  719. indio_dev->modes = INDIO_DIRECT_MODE;
  720. indio_dev->name = chip->client->name;
  721. pm_runtime_enable(&clientp->dev);
  722. pm_runtime_set_autosuspend_delay(&clientp->dev,
  723. TSL2583_POWER_OFF_DELAY_MS);
  724. pm_runtime_use_autosuspend(&clientp->dev);
  725. ret = iio_device_register(indio_dev);
  726. if (ret) {
  727. dev_err(&clientp->dev, "%s: iio registration failed\n",
  728. __func__);
  729. return ret;
  730. }
  731. /* Load up the V2 defaults (these are hard coded defaults for now) */
  732. tsl2583_defaults(chip);
  733. dev_info(&clientp->dev, "Light sensor found.\n");
  734. return 0;
  735. }
  736. static void tsl2583_remove(struct i2c_client *client)
  737. {
  738. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  739. struct tsl2583_chip *chip = iio_priv(indio_dev);
  740. iio_device_unregister(indio_dev);
  741. pm_runtime_disable(&client->dev);
  742. pm_runtime_set_suspended(&client->dev);
  743. tsl2583_set_power_state(chip, TSL2583_CNTL_PWR_OFF);
  744. }
  745. static int tsl2583_suspend(struct device *dev)
  746. {
  747. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  748. struct tsl2583_chip *chip = iio_priv(indio_dev);
  749. int ret;
  750. mutex_lock(&chip->als_mutex);
  751. ret = tsl2583_set_power_state(chip, TSL2583_CNTL_PWR_OFF);
  752. mutex_unlock(&chip->als_mutex);
  753. return ret;
  754. }
  755. static int tsl2583_resume(struct device *dev)
  756. {
  757. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  758. struct tsl2583_chip *chip = iio_priv(indio_dev);
  759. int ret;
  760. mutex_lock(&chip->als_mutex);
  761. ret = tsl2583_chip_init_and_power_on(indio_dev);
  762. mutex_unlock(&chip->als_mutex);
  763. return ret;
  764. }
  765. static DEFINE_RUNTIME_DEV_PM_OPS(tsl2583_pm_ops, tsl2583_suspend,
  766. tsl2583_resume, NULL);
  767. static const struct i2c_device_id tsl2583_idtable[] = {
  768. { "tsl2580", 0 },
  769. { "tsl2581", 1 },
  770. { "tsl2583", 2 },
  771. {}
  772. };
  773. MODULE_DEVICE_TABLE(i2c, tsl2583_idtable);
  774. static const struct of_device_id tsl2583_of_match[] = {
  775. { .compatible = "amstaos,tsl2580", },
  776. { .compatible = "amstaos,tsl2581", },
  777. { .compatible = "amstaos,tsl2583", },
  778. { },
  779. };
  780. MODULE_DEVICE_TABLE(of, tsl2583_of_match);
  781. /* Driver definition */
  782. static struct i2c_driver tsl2583_driver = {
  783. .driver = {
  784. .name = "tsl2583",
  785. .pm = pm_ptr(&tsl2583_pm_ops),
  786. .of_match_table = tsl2583_of_match,
  787. },
  788. .id_table = tsl2583_idtable,
  789. .probe = tsl2583_probe,
  790. .remove = tsl2583_remove,
  791. };
  792. module_i2c_driver(tsl2583_driver);
  793. MODULE_AUTHOR("J. August Brenner <[email protected]>");
  794. MODULE_AUTHOR("Brian Masney <[email protected]>");
  795. MODULE_DESCRIPTION("TAOS tsl2583 ambient light sensor driver");
  796. MODULE_LICENSE("GPL");