tsl2563.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * drivers/iio/light/tsl2563.c
  4. *
  5. * Copyright (C) 2008 Nokia Corporation
  6. *
  7. * Written by Timo O. Karjalainen <[email protected]>
  8. * Contact: Amit Kucheria <[email protected]>
  9. *
  10. * Converted to IIO driver
  11. * Amit Kucheria <[email protected]>
  12. */
  13. #include <linux/module.h>
  14. #include <linux/mod_devicetable.h>
  15. #include <linux/property.h>
  16. #include <linux/i2c.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/irq.h>
  19. #include <linux/sched.h>
  20. #include <linux/mutex.h>
  21. #include <linux/delay.h>
  22. #include <linux/pm.h>
  23. #include <linux/err.h>
  24. #include <linux/slab.h>
  25. #include <linux/iio/iio.h>
  26. #include <linux/iio/sysfs.h>
  27. #include <linux/iio/events.h>
  28. #include <linux/platform_data/tsl2563.h>
  29. /* Use this many bits for fraction part. */
  30. #define ADC_FRAC_BITS 14
  31. /* Given number of 1/10000's in ADC_FRAC_BITS precision. */
  32. #define FRAC10K(f) (((f) * (1L << (ADC_FRAC_BITS))) / (10000))
  33. /* Bits used for fraction in calibration coefficients.*/
  34. #define CALIB_FRAC_BITS 10
  35. /* 0.5 in CALIB_FRAC_BITS precision */
  36. #define CALIB_FRAC_HALF (1 << (CALIB_FRAC_BITS - 1))
  37. /* Make a fraction from a number n that was multiplied with b. */
  38. #define CALIB_FRAC(n, b) (((n) << CALIB_FRAC_BITS) / (b))
  39. /* Decimal 10^(digits in sysfs presentation) */
  40. #define CALIB_BASE_SYSFS 1000
  41. #define TSL2563_CMD 0x80
  42. #define TSL2563_CLEARINT 0x40
  43. #define TSL2563_REG_CTRL 0x00
  44. #define TSL2563_REG_TIMING 0x01
  45. #define TSL2563_REG_LOWLOW 0x02 /* data0 low threshold, 2 bytes */
  46. #define TSL2563_REG_LOWHIGH 0x03
  47. #define TSL2563_REG_HIGHLOW 0x04 /* data0 high threshold, 2 bytes */
  48. #define TSL2563_REG_HIGHHIGH 0x05
  49. #define TSL2563_REG_INT 0x06
  50. #define TSL2563_REG_ID 0x0a
  51. #define TSL2563_REG_DATA0LOW 0x0c /* broadband sensor value, 2 bytes */
  52. #define TSL2563_REG_DATA0HIGH 0x0d
  53. #define TSL2563_REG_DATA1LOW 0x0e /* infrared sensor value, 2 bytes */
  54. #define TSL2563_REG_DATA1HIGH 0x0f
  55. #define TSL2563_CMD_POWER_ON 0x03
  56. #define TSL2563_CMD_POWER_OFF 0x00
  57. #define TSL2563_CTRL_POWER_MASK 0x03
  58. #define TSL2563_TIMING_13MS 0x00
  59. #define TSL2563_TIMING_100MS 0x01
  60. #define TSL2563_TIMING_400MS 0x02
  61. #define TSL2563_TIMING_MASK 0x03
  62. #define TSL2563_TIMING_GAIN16 0x10
  63. #define TSL2563_TIMING_GAIN1 0x00
  64. #define TSL2563_INT_DISABLED 0x00
  65. #define TSL2563_INT_LEVEL 0x10
  66. #define TSL2563_INT_PERSIST(n) ((n) & 0x0F)
  67. struct tsl2563_gainlevel_coeff {
  68. u8 gaintime;
  69. u16 min;
  70. u16 max;
  71. };
  72. static const struct tsl2563_gainlevel_coeff tsl2563_gainlevel_table[] = {
  73. {
  74. .gaintime = TSL2563_TIMING_400MS | TSL2563_TIMING_GAIN16,
  75. .min = 0,
  76. .max = 65534,
  77. }, {
  78. .gaintime = TSL2563_TIMING_400MS | TSL2563_TIMING_GAIN1,
  79. .min = 2048,
  80. .max = 65534,
  81. }, {
  82. .gaintime = TSL2563_TIMING_100MS | TSL2563_TIMING_GAIN1,
  83. .min = 4095,
  84. .max = 37177,
  85. }, {
  86. .gaintime = TSL2563_TIMING_13MS | TSL2563_TIMING_GAIN1,
  87. .min = 3000,
  88. .max = 65535,
  89. },
  90. };
  91. struct tsl2563_chip {
  92. struct mutex lock;
  93. struct i2c_client *client;
  94. struct delayed_work poweroff_work;
  95. /* Remember state for suspend and resume functions */
  96. bool suspended;
  97. struct tsl2563_gainlevel_coeff const *gainlevel;
  98. u16 low_thres;
  99. u16 high_thres;
  100. u8 intr;
  101. bool int_enabled;
  102. /* Calibration coefficients */
  103. u32 calib0;
  104. u32 calib1;
  105. int cover_comp_gain;
  106. /* Cache current values, to be returned while suspended */
  107. u32 data0;
  108. u32 data1;
  109. };
  110. static int tsl2563_set_power(struct tsl2563_chip *chip, int on)
  111. {
  112. struct i2c_client *client = chip->client;
  113. u8 cmd;
  114. cmd = on ? TSL2563_CMD_POWER_ON : TSL2563_CMD_POWER_OFF;
  115. return i2c_smbus_write_byte_data(client,
  116. TSL2563_CMD | TSL2563_REG_CTRL, cmd);
  117. }
  118. /*
  119. * Return value is 0 for off, 1 for on, or a negative error
  120. * code if reading failed.
  121. */
  122. static int tsl2563_get_power(struct tsl2563_chip *chip)
  123. {
  124. struct i2c_client *client = chip->client;
  125. int ret;
  126. ret = i2c_smbus_read_byte_data(client, TSL2563_CMD | TSL2563_REG_CTRL);
  127. if (ret < 0)
  128. return ret;
  129. return (ret & TSL2563_CTRL_POWER_MASK) == TSL2563_CMD_POWER_ON;
  130. }
  131. static int tsl2563_configure(struct tsl2563_chip *chip)
  132. {
  133. int ret;
  134. ret = i2c_smbus_write_byte_data(chip->client,
  135. TSL2563_CMD | TSL2563_REG_TIMING,
  136. chip->gainlevel->gaintime);
  137. if (ret)
  138. goto error_ret;
  139. ret = i2c_smbus_write_byte_data(chip->client,
  140. TSL2563_CMD | TSL2563_REG_HIGHLOW,
  141. chip->high_thres & 0xFF);
  142. if (ret)
  143. goto error_ret;
  144. ret = i2c_smbus_write_byte_data(chip->client,
  145. TSL2563_CMD | TSL2563_REG_HIGHHIGH,
  146. (chip->high_thres >> 8) & 0xFF);
  147. if (ret)
  148. goto error_ret;
  149. ret = i2c_smbus_write_byte_data(chip->client,
  150. TSL2563_CMD | TSL2563_REG_LOWLOW,
  151. chip->low_thres & 0xFF);
  152. if (ret)
  153. goto error_ret;
  154. ret = i2c_smbus_write_byte_data(chip->client,
  155. TSL2563_CMD | TSL2563_REG_LOWHIGH,
  156. (chip->low_thres >> 8) & 0xFF);
  157. /*
  158. * Interrupt register is automatically written anyway if it is relevant
  159. * so is not here.
  160. */
  161. error_ret:
  162. return ret;
  163. }
  164. static void tsl2563_poweroff_work(struct work_struct *work)
  165. {
  166. struct tsl2563_chip *chip =
  167. container_of(work, struct tsl2563_chip, poweroff_work.work);
  168. tsl2563_set_power(chip, 0);
  169. }
  170. static int tsl2563_detect(struct tsl2563_chip *chip)
  171. {
  172. int ret;
  173. ret = tsl2563_set_power(chip, 1);
  174. if (ret)
  175. return ret;
  176. ret = tsl2563_get_power(chip);
  177. if (ret < 0)
  178. return ret;
  179. return ret ? 0 : -ENODEV;
  180. }
  181. static int tsl2563_read_id(struct tsl2563_chip *chip, u8 *id)
  182. {
  183. struct i2c_client *client = chip->client;
  184. int ret;
  185. ret = i2c_smbus_read_byte_data(client, TSL2563_CMD | TSL2563_REG_ID);
  186. if (ret < 0)
  187. return ret;
  188. *id = ret;
  189. return 0;
  190. }
  191. /*
  192. * "Normalized" ADC value is one obtained with 400ms of integration time and
  193. * 16x gain. This function returns the number of bits of shift needed to
  194. * convert between normalized values and HW values obtained using given
  195. * timing and gain settings.
  196. */
  197. static int tsl2563_adc_shiftbits(u8 timing)
  198. {
  199. int shift = 0;
  200. switch (timing & TSL2563_TIMING_MASK) {
  201. case TSL2563_TIMING_13MS:
  202. shift += 5;
  203. break;
  204. case TSL2563_TIMING_100MS:
  205. shift += 2;
  206. break;
  207. case TSL2563_TIMING_400MS:
  208. /* no-op */
  209. break;
  210. }
  211. if (!(timing & TSL2563_TIMING_GAIN16))
  212. shift += 4;
  213. return shift;
  214. }
  215. /* Convert a HW ADC value to normalized scale. */
  216. static u32 tsl2563_normalize_adc(u16 adc, u8 timing)
  217. {
  218. return adc << tsl2563_adc_shiftbits(timing);
  219. }
  220. static void tsl2563_wait_adc(struct tsl2563_chip *chip)
  221. {
  222. unsigned int delay;
  223. switch (chip->gainlevel->gaintime & TSL2563_TIMING_MASK) {
  224. case TSL2563_TIMING_13MS:
  225. delay = 14;
  226. break;
  227. case TSL2563_TIMING_100MS:
  228. delay = 101;
  229. break;
  230. default:
  231. delay = 402;
  232. }
  233. /*
  234. * TODO: Make sure that we wait at least required delay but why we
  235. * have to extend it one tick more?
  236. */
  237. schedule_timeout_interruptible(msecs_to_jiffies(delay) + 2);
  238. }
  239. static int tsl2563_adjust_gainlevel(struct tsl2563_chip *chip, u16 adc)
  240. {
  241. struct i2c_client *client = chip->client;
  242. if (adc > chip->gainlevel->max || adc < chip->gainlevel->min) {
  243. (adc > chip->gainlevel->max) ?
  244. chip->gainlevel++ : chip->gainlevel--;
  245. i2c_smbus_write_byte_data(client,
  246. TSL2563_CMD | TSL2563_REG_TIMING,
  247. chip->gainlevel->gaintime);
  248. tsl2563_wait_adc(chip);
  249. tsl2563_wait_adc(chip);
  250. return 1;
  251. } else
  252. return 0;
  253. }
  254. static int tsl2563_get_adc(struct tsl2563_chip *chip)
  255. {
  256. struct i2c_client *client = chip->client;
  257. u16 adc0, adc1;
  258. int retry = 1;
  259. int ret = 0;
  260. if (chip->suspended)
  261. goto out;
  262. if (!chip->int_enabled) {
  263. cancel_delayed_work_sync(&chip->poweroff_work);
  264. if (!tsl2563_get_power(chip)) {
  265. ret = tsl2563_set_power(chip, 1);
  266. if (ret)
  267. goto out;
  268. ret = tsl2563_configure(chip);
  269. if (ret)
  270. goto out;
  271. tsl2563_wait_adc(chip);
  272. }
  273. }
  274. while (retry) {
  275. ret = i2c_smbus_read_word_data(client,
  276. TSL2563_CMD | TSL2563_REG_DATA0LOW);
  277. if (ret < 0)
  278. goto out;
  279. adc0 = ret;
  280. ret = i2c_smbus_read_word_data(client,
  281. TSL2563_CMD | TSL2563_REG_DATA1LOW);
  282. if (ret < 0)
  283. goto out;
  284. adc1 = ret;
  285. retry = tsl2563_adjust_gainlevel(chip, adc0);
  286. }
  287. chip->data0 = tsl2563_normalize_adc(adc0, chip->gainlevel->gaintime);
  288. chip->data1 = tsl2563_normalize_adc(adc1, chip->gainlevel->gaintime);
  289. if (!chip->int_enabled)
  290. schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
  291. ret = 0;
  292. out:
  293. return ret;
  294. }
  295. static inline int tsl2563_calib_to_sysfs(u32 calib)
  296. {
  297. return (int) (((calib * CALIB_BASE_SYSFS) +
  298. CALIB_FRAC_HALF) >> CALIB_FRAC_BITS);
  299. }
  300. static inline u32 tsl2563_calib_from_sysfs(int value)
  301. {
  302. return (((u32) value) << CALIB_FRAC_BITS) / CALIB_BASE_SYSFS;
  303. }
  304. /*
  305. * Conversions between lux and ADC values.
  306. *
  307. * The basic formula is lux = c0 * adc0 - c1 * adc1, where c0 and c1 are
  308. * appropriate constants. Different constants are needed for different
  309. * kinds of light, determined by the ratio adc1/adc0 (basically the ratio
  310. * of the intensities in infrared and visible wavelengths). lux_table below
  311. * lists the upper threshold of the adc1/adc0 ratio and the corresponding
  312. * constants.
  313. */
  314. struct tsl2563_lux_coeff {
  315. unsigned long ch_ratio;
  316. unsigned long ch0_coeff;
  317. unsigned long ch1_coeff;
  318. };
  319. static const struct tsl2563_lux_coeff lux_table[] = {
  320. {
  321. .ch_ratio = FRAC10K(1300),
  322. .ch0_coeff = FRAC10K(315),
  323. .ch1_coeff = FRAC10K(262),
  324. }, {
  325. .ch_ratio = FRAC10K(2600),
  326. .ch0_coeff = FRAC10K(337),
  327. .ch1_coeff = FRAC10K(430),
  328. }, {
  329. .ch_ratio = FRAC10K(3900),
  330. .ch0_coeff = FRAC10K(363),
  331. .ch1_coeff = FRAC10K(529),
  332. }, {
  333. .ch_ratio = FRAC10K(5200),
  334. .ch0_coeff = FRAC10K(392),
  335. .ch1_coeff = FRAC10K(605),
  336. }, {
  337. .ch_ratio = FRAC10K(6500),
  338. .ch0_coeff = FRAC10K(229),
  339. .ch1_coeff = FRAC10K(291),
  340. }, {
  341. .ch_ratio = FRAC10K(8000),
  342. .ch0_coeff = FRAC10K(157),
  343. .ch1_coeff = FRAC10K(180),
  344. }, {
  345. .ch_ratio = FRAC10K(13000),
  346. .ch0_coeff = FRAC10K(34),
  347. .ch1_coeff = FRAC10K(26),
  348. }, {
  349. .ch_ratio = ULONG_MAX,
  350. .ch0_coeff = 0,
  351. .ch1_coeff = 0,
  352. },
  353. };
  354. /* Convert normalized, scaled ADC values to lux. */
  355. static unsigned int tsl2563_adc_to_lux(u32 adc0, u32 adc1)
  356. {
  357. const struct tsl2563_lux_coeff *lp = lux_table;
  358. unsigned long ratio, lux, ch0 = adc0, ch1 = adc1;
  359. ratio = ch0 ? ((ch1 << ADC_FRAC_BITS) / ch0) : ULONG_MAX;
  360. while (lp->ch_ratio < ratio)
  361. lp++;
  362. lux = ch0 * lp->ch0_coeff - ch1 * lp->ch1_coeff;
  363. return (unsigned int) (lux >> ADC_FRAC_BITS);
  364. }
  365. /* Apply calibration coefficient to ADC count. */
  366. static u32 tsl2563_calib_adc(u32 adc, u32 calib)
  367. {
  368. unsigned long scaled = adc;
  369. scaled *= calib;
  370. scaled >>= CALIB_FRAC_BITS;
  371. return (u32) scaled;
  372. }
  373. static int tsl2563_write_raw(struct iio_dev *indio_dev,
  374. struct iio_chan_spec const *chan,
  375. int val,
  376. int val2,
  377. long mask)
  378. {
  379. struct tsl2563_chip *chip = iio_priv(indio_dev);
  380. if (mask != IIO_CHAN_INFO_CALIBSCALE)
  381. return -EINVAL;
  382. if (chan->channel2 == IIO_MOD_LIGHT_BOTH)
  383. chip->calib0 = tsl2563_calib_from_sysfs(val);
  384. else if (chan->channel2 == IIO_MOD_LIGHT_IR)
  385. chip->calib1 = tsl2563_calib_from_sysfs(val);
  386. else
  387. return -EINVAL;
  388. return 0;
  389. }
  390. static int tsl2563_read_raw(struct iio_dev *indio_dev,
  391. struct iio_chan_spec const *chan,
  392. int *val,
  393. int *val2,
  394. long mask)
  395. {
  396. int ret = -EINVAL;
  397. u32 calib0, calib1;
  398. struct tsl2563_chip *chip = iio_priv(indio_dev);
  399. mutex_lock(&chip->lock);
  400. switch (mask) {
  401. case IIO_CHAN_INFO_RAW:
  402. case IIO_CHAN_INFO_PROCESSED:
  403. switch (chan->type) {
  404. case IIO_LIGHT:
  405. ret = tsl2563_get_adc(chip);
  406. if (ret)
  407. goto error_ret;
  408. calib0 = tsl2563_calib_adc(chip->data0, chip->calib0) *
  409. chip->cover_comp_gain;
  410. calib1 = tsl2563_calib_adc(chip->data1, chip->calib1) *
  411. chip->cover_comp_gain;
  412. *val = tsl2563_adc_to_lux(calib0, calib1);
  413. ret = IIO_VAL_INT;
  414. break;
  415. case IIO_INTENSITY:
  416. ret = tsl2563_get_adc(chip);
  417. if (ret)
  418. goto error_ret;
  419. if (chan->channel2 == IIO_MOD_LIGHT_BOTH)
  420. *val = chip->data0;
  421. else
  422. *val = chip->data1;
  423. ret = IIO_VAL_INT;
  424. break;
  425. default:
  426. break;
  427. }
  428. break;
  429. case IIO_CHAN_INFO_CALIBSCALE:
  430. if (chan->channel2 == IIO_MOD_LIGHT_BOTH)
  431. *val = tsl2563_calib_to_sysfs(chip->calib0);
  432. else
  433. *val = tsl2563_calib_to_sysfs(chip->calib1);
  434. ret = IIO_VAL_INT;
  435. break;
  436. default:
  437. ret = -EINVAL;
  438. goto error_ret;
  439. }
  440. error_ret:
  441. mutex_unlock(&chip->lock);
  442. return ret;
  443. }
  444. static const struct iio_event_spec tsl2563_events[] = {
  445. {
  446. .type = IIO_EV_TYPE_THRESH,
  447. .dir = IIO_EV_DIR_RISING,
  448. .mask_separate = BIT(IIO_EV_INFO_VALUE) |
  449. BIT(IIO_EV_INFO_ENABLE),
  450. }, {
  451. .type = IIO_EV_TYPE_THRESH,
  452. .dir = IIO_EV_DIR_FALLING,
  453. .mask_separate = BIT(IIO_EV_INFO_VALUE) |
  454. BIT(IIO_EV_INFO_ENABLE),
  455. },
  456. };
  457. static const struct iio_chan_spec tsl2563_channels[] = {
  458. {
  459. .type = IIO_LIGHT,
  460. .indexed = 1,
  461. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
  462. .channel = 0,
  463. }, {
  464. .type = IIO_INTENSITY,
  465. .modified = 1,
  466. .channel2 = IIO_MOD_LIGHT_BOTH,
  467. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  468. BIT(IIO_CHAN_INFO_CALIBSCALE),
  469. .event_spec = tsl2563_events,
  470. .num_event_specs = ARRAY_SIZE(tsl2563_events),
  471. }, {
  472. .type = IIO_INTENSITY,
  473. .modified = 1,
  474. .channel2 = IIO_MOD_LIGHT_IR,
  475. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  476. BIT(IIO_CHAN_INFO_CALIBSCALE),
  477. }
  478. };
  479. static int tsl2563_read_thresh(struct iio_dev *indio_dev,
  480. const struct iio_chan_spec *chan, enum iio_event_type type,
  481. enum iio_event_direction dir, enum iio_event_info info, int *val,
  482. int *val2)
  483. {
  484. struct tsl2563_chip *chip = iio_priv(indio_dev);
  485. switch (dir) {
  486. case IIO_EV_DIR_RISING:
  487. *val = chip->high_thres;
  488. break;
  489. case IIO_EV_DIR_FALLING:
  490. *val = chip->low_thres;
  491. break;
  492. default:
  493. return -EINVAL;
  494. }
  495. return IIO_VAL_INT;
  496. }
  497. static int tsl2563_write_thresh(struct iio_dev *indio_dev,
  498. const struct iio_chan_spec *chan, enum iio_event_type type,
  499. enum iio_event_direction dir, enum iio_event_info info, int val,
  500. int val2)
  501. {
  502. struct tsl2563_chip *chip = iio_priv(indio_dev);
  503. int ret;
  504. u8 address;
  505. if (dir == IIO_EV_DIR_RISING)
  506. address = TSL2563_REG_HIGHLOW;
  507. else
  508. address = TSL2563_REG_LOWLOW;
  509. mutex_lock(&chip->lock);
  510. ret = i2c_smbus_write_byte_data(chip->client, TSL2563_CMD | address,
  511. val & 0xFF);
  512. if (ret)
  513. goto error_ret;
  514. ret = i2c_smbus_write_byte_data(chip->client,
  515. TSL2563_CMD | (address + 1),
  516. (val >> 8) & 0xFF);
  517. if (dir == IIO_EV_DIR_RISING)
  518. chip->high_thres = val;
  519. else
  520. chip->low_thres = val;
  521. error_ret:
  522. mutex_unlock(&chip->lock);
  523. return ret;
  524. }
  525. static irqreturn_t tsl2563_event_handler(int irq, void *private)
  526. {
  527. struct iio_dev *dev_info = private;
  528. struct tsl2563_chip *chip = iio_priv(dev_info);
  529. iio_push_event(dev_info,
  530. IIO_UNMOD_EVENT_CODE(IIO_INTENSITY,
  531. 0,
  532. IIO_EV_TYPE_THRESH,
  533. IIO_EV_DIR_EITHER),
  534. iio_get_time_ns(dev_info));
  535. /* clear the interrupt and push the event */
  536. i2c_smbus_write_byte(chip->client, TSL2563_CMD | TSL2563_CLEARINT);
  537. return IRQ_HANDLED;
  538. }
  539. static int tsl2563_write_interrupt_config(struct iio_dev *indio_dev,
  540. const struct iio_chan_spec *chan, enum iio_event_type type,
  541. enum iio_event_direction dir, int state)
  542. {
  543. struct tsl2563_chip *chip = iio_priv(indio_dev);
  544. int ret = 0;
  545. mutex_lock(&chip->lock);
  546. if (state && !(chip->intr & 0x30)) {
  547. chip->intr &= ~0x30;
  548. chip->intr |= 0x10;
  549. /* ensure the chip is actually on */
  550. cancel_delayed_work_sync(&chip->poweroff_work);
  551. if (!tsl2563_get_power(chip)) {
  552. ret = tsl2563_set_power(chip, 1);
  553. if (ret)
  554. goto out;
  555. ret = tsl2563_configure(chip);
  556. if (ret)
  557. goto out;
  558. }
  559. ret = i2c_smbus_write_byte_data(chip->client,
  560. TSL2563_CMD | TSL2563_REG_INT,
  561. chip->intr);
  562. chip->int_enabled = true;
  563. }
  564. if (!state && (chip->intr & 0x30)) {
  565. chip->intr &= ~0x30;
  566. ret = i2c_smbus_write_byte_data(chip->client,
  567. TSL2563_CMD | TSL2563_REG_INT,
  568. chip->intr);
  569. chip->int_enabled = false;
  570. /* now the interrupt is not enabled, we can go to sleep */
  571. schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
  572. }
  573. out:
  574. mutex_unlock(&chip->lock);
  575. return ret;
  576. }
  577. static int tsl2563_read_interrupt_config(struct iio_dev *indio_dev,
  578. const struct iio_chan_spec *chan, enum iio_event_type type,
  579. enum iio_event_direction dir)
  580. {
  581. struct tsl2563_chip *chip = iio_priv(indio_dev);
  582. int ret;
  583. mutex_lock(&chip->lock);
  584. ret = i2c_smbus_read_byte_data(chip->client,
  585. TSL2563_CMD | TSL2563_REG_INT);
  586. mutex_unlock(&chip->lock);
  587. if (ret < 0)
  588. return ret;
  589. return !!(ret & 0x30);
  590. }
  591. static const struct iio_info tsl2563_info_no_irq = {
  592. .read_raw = &tsl2563_read_raw,
  593. .write_raw = &tsl2563_write_raw,
  594. };
  595. static const struct iio_info tsl2563_info = {
  596. .read_raw = &tsl2563_read_raw,
  597. .write_raw = &tsl2563_write_raw,
  598. .read_event_value = &tsl2563_read_thresh,
  599. .write_event_value = &tsl2563_write_thresh,
  600. .read_event_config = &tsl2563_read_interrupt_config,
  601. .write_event_config = &tsl2563_write_interrupt_config,
  602. };
  603. static int tsl2563_probe(struct i2c_client *client,
  604. const struct i2c_device_id *device_id)
  605. {
  606. struct iio_dev *indio_dev;
  607. struct tsl2563_chip *chip;
  608. struct tsl2563_platform_data *pdata = client->dev.platform_data;
  609. unsigned long irq_flags;
  610. int err = 0;
  611. u8 id = 0;
  612. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
  613. if (!indio_dev)
  614. return -ENOMEM;
  615. chip = iio_priv(indio_dev);
  616. i2c_set_clientdata(client, indio_dev);
  617. chip->client = client;
  618. err = tsl2563_detect(chip);
  619. if (err) {
  620. dev_err(&client->dev, "detect error %d\n", -err);
  621. return err;
  622. }
  623. err = tsl2563_read_id(chip, &id);
  624. if (err) {
  625. dev_err(&client->dev, "read id error %d\n", -err);
  626. return err;
  627. }
  628. mutex_init(&chip->lock);
  629. /* Default values used until userspace says otherwise */
  630. chip->low_thres = 0x0;
  631. chip->high_thres = 0xffff;
  632. chip->gainlevel = tsl2563_gainlevel_table;
  633. chip->intr = TSL2563_INT_PERSIST(4);
  634. chip->calib0 = tsl2563_calib_from_sysfs(CALIB_BASE_SYSFS);
  635. chip->calib1 = tsl2563_calib_from_sysfs(CALIB_BASE_SYSFS);
  636. if (pdata) {
  637. chip->cover_comp_gain = pdata->cover_comp_gain;
  638. } else {
  639. err = device_property_read_u32(&client->dev, "amstaos,cover-comp-gain",
  640. &chip->cover_comp_gain);
  641. if (err)
  642. chip->cover_comp_gain = 1;
  643. }
  644. dev_info(&client->dev, "model %d, rev. %d\n", id >> 4, id & 0x0f);
  645. indio_dev->name = client->name;
  646. indio_dev->channels = tsl2563_channels;
  647. indio_dev->num_channels = ARRAY_SIZE(tsl2563_channels);
  648. indio_dev->modes = INDIO_DIRECT_MODE;
  649. if (client->irq)
  650. indio_dev->info = &tsl2563_info;
  651. else
  652. indio_dev->info = &tsl2563_info_no_irq;
  653. if (client->irq) {
  654. irq_flags = irq_get_trigger_type(client->irq);
  655. if (irq_flags == IRQF_TRIGGER_NONE)
  656. irq_flags = IRQF_TRIGGER_RISING;
  657. irq_flags |= IRQF_ONESHOT;
  658. err = devm_request_threaded_irq(&client->dev, client->irq,
  659. NULL,
  660. &tsl2563_event_handler,
  661. irq_flags,
  662. "tsl2563_event",
  663. indio_dev);
  664. if (err) {
  665. dev_err(&client->dev, "irq request error %d\n", -err);
  666. return err;
  667. }
  668. }
  669. err = tsl2563_configure(chip);
  670. if (err) {
  671. dev_err(&client->dev, "configure error %d\n", -err);
  672. return err;
  673. }
  674. INIT_DELAYED_WORK(&chip->poweroff_work, tsl2563_poweroff_work);
  675. /* The interrupt cannot yet be enabled so this is fine without lock */
  676. schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
  677. err = iio_device_register(indio_dev);
  678. if (err) {
  679. dev_err(&client->dev, "iio registration error %d\n", -err);
  680. goto fail;
  681. }
  682. return 0;
  683. fail:
  684. cancel_delayed_work_sync(&chip->poweroff_work);
  685. return err;
  686. }
  687. static void tsl2563_remove(struct i2c_client *client)
  688. {
  689. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  690. struct tsl2563_chip *chip = iio_priv(indio_dev);
  691. iio_device_unregister(indio_dev);
  692. if (!chip->int_enabled)
  693. cancel_delayed_work_sync(&chip->poweroff_work);
  694. /* Ensure that interrupts are disabled - then flush any bottom halves */
  695. chip->intr &= ~0x30;
  696. i2c_smbus_write_byte_data(chip->client, TSL2563_CMD | TSL2563_REG_INT,
  697. chip->intr);
  698. tsl2563_set_power(chip, 0);
  699. }
  700. static int tsl2563_suspend(struct device *dev)
  701. {
  702. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  703. struct tsl2563_chip *chip = iio_priv(indio_dev);
  704. int ret;
  705. mutex_lock(&chip->lock);
  706. ret = tsl2563_set_power(chip, 0);
  707. if (ret)
  708. goto out;
  709. chip->suspended = true;
  710. out:
  711. mutex_unlock(&chip->lock);
  712. return ret;
  713. }
  714. static int tsl2563_resume(struct device *dev)
  715. {
  716. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  717. struct tsl2563_chip *chip = iio_priv(indio_dev);
  718. int ret;
  719. mutex_lock(&chip->lock);
  720. ret = tsl2563_set_power(chip, 1);
  721. if (ret)
  722. goto out;
  723. ret = tsl2563_configure(chip);
  724. if (ret)
  725. goto out;
  726. chip->suspended = false;
  727. out:
  728. mutex_unlock(&chip->lock);
  729. return ret;
  730. }
  731. static DEFINE_SIMPLE_DEV_PM_OPS(tsl2563_pm_ops, tsl2563_suspend,
  732. tsl2563_resume);
  733. static const struct i2c_device_id tsl2563_id[] = {
  734. { "tsl2560", 0 },
  735. { "tsl2561", 1 },
  736. { "tsl2562", 2 },
  737. { "tsl2563", 3 },
  738. {}
  739. };
  740. MODULE_DEVICE_TABLE(i2c, tsl2563_id);
  741. static const struct of_device_id tsl2563_of_match[] = {
  742. { .compatible = "amstaos,tsl2560" },
  743. { .compatible = "amstaos,tsl2561" },
  744. { .compatible = "amstaos,tsl2562" },
  745. { .compatible = "amstaos,tsl2563" },
  746. {}
  747. };
  748. MODULE_DEVICE_TABLE(of, tsl2563_of_match);
  749. static struct i2c_driver tsl2563_i2c_driver = {
  750. .driver = {
  751. .name = "tsl2563",
  752. .of_match_table = tsl2563_of_match,
  753. .pm = pm_sleep_ptr(&tsl2563_pm_ops),
  754. },
  755. .probe = tsl2563_probe,
  756. .remove = tsl2563_remove,
  757. .id_table = tsl2563_id,
  758. };
  759. module_i2c_driver(tsl2563_i2c_driver);
  760. MODULE_AUTHOR("Nokia Corporation");
  761. MODULE_DESCRIPTION("tsl2563 light sensor driver");
  762. MODULE_LICENSE("GPL");