tsl2563.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  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 <timo.o.karjalainen@nokia.com>
  8. * Contact: Amit Kucheria <amit.kucheria@verdurent.com>
  9. *
  10. * Converted to IIO driver
  11. * Amit Kucheria <amit.kucheria@verdurent.com>
  12. */
  13. #include <linux/bits.h>
  14. #include <linux/delay.h>
  15. #include <linux/err.h>
  16. #include <linux/i2c.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/irq.h>
  19. #include <linux/math.h>
  20. #include <linux/mod_devicetable.h>
  21. #include <linux/module.h>
  22. #include <linux/mutex.h>
  23. #include <linux/pm.h>
  24. #include <linux/property.h>
  25. #include <linux/sched.h>
  26. #include <linux/slab.h>
  27. #include <linux/iio/events.h>
  28. #include <linux/iio/iio.h>
  29. #include <linux/iio/sysfs.h>
  30. /* Use this many bits for fraction part. */
  31. #define ADC_FRAC_BITS 14
  32. /* Given number of 1/10000's in ADC_FRAC_BITS precision. */
  33. #define FRAC10K(f) (((f) * BIT(ADC_FRAC_BITS)) / (10000))
  34. /* Bits used for fraction in calibration coefficients.*/
  35. #define CALIB_FRAC_BITS 10
  36. /* Decimal 10^(digits in sysfs presentation) */
  37. #define CALIB_BASE_SYSFS 1000
  38. #define TSL2563_CMD BIT(7)
  39. #define TSL2563_CLEARINT BIT(6)
  40. #define TSL2563_REG_CTRL 0x00
  41. #define TSL2563_REG_TIMING 0x01
  42. #define TSL2563_REG_LOW 0x02 /* data0 low threshold, 2 bytes */
  43. #define TSL2563_REG_HIGH 0x04 /* data0 high threshold, 2 bytes */
  44. #define TSL2563_REG_INT 0x06
  45. #define TSL2563_REG_ID 0x0a
  46. #define TSL2563_REG_DATA0 0x0c /* broadband sensor value, 2 bytes */
  47. #define TSL2563_REG_DATA1 0x0e /* infrared sensor value, 2 bytes */
  48. #define TSL2563_CMD_POWER_ON 0x03
  49. #define TSL2563_CMD_POWER_OFF 0x00
  50. #define TSL2563_CTRL_POWER_MASK GENMASK(1, 0)
  51. #define TSL2563_TIMING_13MS 0x00
  52. #define TSL2563_TIMING_100MS 0x01
  53. #define TSL2563_TIMING_400MS 0x02
  54. #define TSL2563_TIMING_MASK GENMASK(1, 0)
  55. #define TSL2563_TIMING_GAIN16 0x10
  56. #define TSL2563_TIMING_GAIN1 0x00
  57. #define TSL2563_INT_DISABLED 0x00
  58. #define TSL2563_INT_LEVEL 0x10
  59. #define TSL2563_INT_MASK GENMASK(5, 4)
  60. #define TSL2563_INT_PERSIST(n) ((n) & GENMASK(3, 0))
  61. struct tsl2563_gainlevel_coeff {
  62. u8 gaintime;
  63. u16 min;
  64. u16 max;
  65. };
  66. static const struct tsl2563_gainlevel_coeff tsl2563_gainlevel_table[] = {
  67. {
  68. .gaintime = TSL2563_TIMING_400MS | TSL2563_TIMING_GAIN16,
  69. .min = 0,
  70. .max = 65534,
  71. }, {
  72. .gaintime = TSL2563_TIMING_400MS | TSL2563_TIMING_GAIN1,
  73. .min = 2048,
  74. .max = 65534,
  75. }, {
  76. .gaintime = TSL2563_TIMING_100MS | TSL2563_TIMING_GAIN1,
  77. .min = 4095,
  78. .max = 37177,
  79. }, {
  80. .gaintime = TSL2563_TIMING_13MS | TSL2563_TIMING_GAIN1,
  81. .min = 3000,
  82. .max = 65535,
  83. },
  84. };
  85. struct tsl2563_chip {
  86. struct mutex lock;
  87. struct i2c_client *client;
  88. struct delayed_work poweroff_work;
  89. /* Remember state for suspend and resume functions */
  90. bool suspended;
  91. struct tsl2563_gainlevel_coeff const *gainlevel;
  92. u16 low_thres;
  93. u16 high_thres;
  94. u8 intr;
  95. bool int_enabled;
  96. /* Calibration coefficients */
  97. u32 calib0;
  98. u32 calib1;
  99. int cover_comp_gain;
  100. /* Cache current values, to be returned while suspended */
  101. u32 data0;
  102. u32 data1;
  103. };
  104. static int tsl2563_set_power(struct tsl2563_chip *chip, int on)
  105. {
  106. struct i2c_client *client = chip->client;
  107. u8 cmd;
  108. cmd = on ? TSL2563_CMD_POWER_ON : TSL2563_CMD_POWER_OFF;
  109. return i2c_smbus_write_byte_data(client,
  110. TSL2563_CMD | TSL2563_REG_CTRL, cmd);
  111. }
  112. /*
  113. * Return value is 0 for off, 1 for on, or a negative error
  114. * code if reading failed.
  115. */
  116. static int tsl2563_get_power(struct tsl2563_chip *chip)
  117. {
  118. struct i2c_client *client = chip->client;
  119. int ret;
  120. ret = i2c_smbus_read_byte_data(client, TSL2563_CMD | TSL2563_REG_CTRL);
  121. if (ret < 0)
  122. return ret;
  123. return (ret & TSL2563_CTRL_POWER_MASK) == TSL2563_CMD_POWER_ON;
  124. }
  125. static int tsl2563_configure(struct tsl2563_chip *chip)
  126. {
  127. int ret;
  128. ret = i2c_smbus_write_byte_data(chip->client,
  129. TSL2563_CMD | TSL2563_REG_TIMING,
  130. chip->gainlevel->gaintime);
  131. if (ret)
  132. goto error_ret;
  133. ret = i2c_smbus_write_word_data(chip->client,
  134. TSL2563_CMD | TSL2563_REG_HIGH,
  135. chip->high_thres);
  136. if (ret)
  137. goto error_ret;
  138. ret = i2c_smbus_write_word_data(chip->client,
  139. TSL2563_CMD | TSL2563_REG_LOW,
  140. chip->low_thres);
  141. if (ret)
  142. goto error_ret;
  143. /*
  144. * Interrupt register is automatically written anyway if it is relevant
  145. * so is not here.
  146. */
  147. error_ret:
  148. return ret;
  149. }
  150. static void tsl2563_poweroff_work(struct work_struct *work)
  151. {
  152. struct tsl2563_chip *chip =
  153. container_of(work, struct tsl2563_chip, poweroff_work.work);
  154. tsl2563_set_power(chip, 0);
  155. }
  156. static int tsl2563_detect(struct tsl2563_chip *chip)
  157. {
  158. int ret;
  159. ret = tsl2563_set_power(chip, 1);
  160. if (ret)
  161. return ret;
  162. ret = tsl2563_get_power(chip);
  163. if (ret < 0)
  164. return ret;
  165. return ret ? 0 : -ENODEV;
  166. }
  167. static int tsl2563_read_id(struct tsl2563_chip *chip, u8 *id)
  168. {
  169. struct i2c_client *client = chip->client;
  170. int ret;
  171. ret = i2c_smbus_read_byte_data(client, TSL2563_CMD | TSL2563_REG_ID);
  172. if (ret < 0)
  173. return ret;
  174. *id = ret;
  175. return 0;
  176. }
  177. static int tsl2563_configure_irq(struct tsl2563_chip *chip, bool enable)
  178. {
  179. int ret;
  180. chip->intr &= ~TSL2563_INT_MASK;
  181. if (enable)
  182. chip->intr |= TSL2563_INT_LEVEL;
  183. ret = i2c_smbus_write_byte_data(chip->client,
  184. TSL2563_CMD | TSL2563_REG_INT,
  185. chip->intr);
  186. if (ret < 0)
  187. return ret;
  188. chip->int_enabled = enable;
  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_DATA0);
  277. if (ret < 0)
  278. goto out;
  279. adc0 = ret;
  280. ret = i2c_smbus_read_word_data(client,
  281. TSL2563_CMD | TSL2563_REG_DATA1);
  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)DIV_ROUND_CLOSEST(calib * CALIB_BASE_SYSFS, BIT(CALIB_FRAC_BITS));
  298. }
  299. static inline u32 tsl2563_calib_from_sysfs(int value)
  300. {
  301. /* Make a fraction from a number n that was multiplied with b. */
  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. mutex_lock(&chip->lock);
  505. if (dir == IIO_EV_DIR_RISING)
  506. ret = i2c_smbus_write_word_data(chip->client,
  507. TSL2563_CMD | TSL2563_REG_HIGH, val);
  508. else
  509. ret = i2c_smbus_write_word_data(chip->client,
  510. TSL2563_CMD | TSL2563_REG_LOW, val);
  511. if (ret)
  512. goto error_ret;
  513. if (dir == IIO_EV_DIR_RISING)
  514. chip->high_thres = val;
  515. else
  516. chip->low_thres = val;
  517. error_ret:
  518. mutex_unlock(&chip->lock);
  519. return ret;
  520. }
  521. static irqreturn_t tsl2563_event_handler(int irq, void *private)
  522. {
  523. struct iio_dev *dev_info = private;
  524. struct tsl2563_chip *chip = iio_priv(dev_info);
  525. iio_push_event(dev_info,
  526. IIO_UNMOD_EVENT_CODE(IIO_INTENSITY,
  527. 0,
  528. IIO_EV_TYPE_THRESH,
  529. IIO_EV_DIR_EITHER),
  530. iio_get_time_ns(dev_info));
  531. /* clear the interrupt and push the event */
  532. i2c_smbus_write_byte(chip->client, TSL2563_CMD | TSL2563_CLEARINT);
  533. return IRQ_HANDLED;
  534. }
  535. static int tsl2563_write_interrupt_config(struct iio_dev *indio_dev,
  536. const struct iio_chan_spec *chan, enum iio_event_type type,
  537. enum iio_event_direction dir, int state)
  538. {
  539. struct tsl2563_chip *chip = iio_priv(indio_dev);
  540. int ret = 0;
  541. mutex_lock(&chip->lock);
  542. if (state && !(chip->intr & TSL2563_INT_MASK)) {
  543. /* ensure the chip is actually on */
  544. cancel_delayed_work_sync(&chip->poweroff_work);
  545. if (!tsl2563_get_power(chip)) {
  546. ret = tsl2563_set_power(chip, 1);
  547. if (ret)
  548. goto out;
  549. ret = tsl2563_configure(chip);
  550. if (ret)
  551. goto out;
  552. }
  553. ret = tsl2563_configure_irq(chip, true);
  554. }
  555. if (!state && (chip->intr & TSL2563_INT_MASK)) {
  556. ret = tsl2563_configure_irq(chip, false);
  557. /* now the interrupt is not enabled, we can go to sleep */
  558. schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
  559. }
  560. out:
  561. mutex_unlock(&chip->lock);
  562. return ret;
  563. }
  564. static int tsl2563_read_interrupt_config(struct iio_dev *indio_dev,
  565. const struct iio_chan_spec *chan, enum iio_event_type type,
  566. enum iio_event_direction dir)
  567. {
  568. struct tsl2563_chip *chip = iio_priv(indio_dev);
  569. int ret;
  570. mutex_lock(&chip->lock);
  571. ret = i2c_smbus_read_byte_data(chip->client,
  572. TSL2563_CMD | TSL2563_REG_INT);
  573. mutex_unlock(&chip->lock);
  574. if (ret < 0)
  575. return ret;
  576. return !!(ret & TSL2563_INT_MASK);
  577. }
  578. static const struct iio_info tsl2563_info_no_irq = {
  579. .read_raw = &tsl2563_read_raw,
  580. .write_raw = &tsl2563_write_raw,
  581. };
  582. static const struct iio_info tsl2563_info = {
  583. .read_raw = &tsl2563_read_raw,
  584. .write_raw = &tsl2563_write_raw,
  585. .read_event_value = &tsl2563_read_thresh,
  586. .write_event_value = &tsl2563_write_thresh,
  587. .read_event_config = &tsl2563_read_interrupt_config,
  588. .write_event_config = &tsl2563_write_interrupt_config,
  589. };
  590. static int tsl2563_probe(struct i2c_client *client)
  591. {
  592. struct device *dev = &client->dev;
  593. struct iio_dev *indio_dev;
  594. struct tsl2563_chip *chip;
  595. unsigned long irq_flags;
  596. u8 id = 0;
  597. int err;
  598. indio_dev = devm_iio_device_alloc(dev, sizeof(*chip));
  599. if (!indio_dev)
  600. return -ENOMEM;
  601. chip = iio_priv(indio_dev);
  602. i2c_set_clientdata(client, indio_dev);
  603. chip->client = client;
  604. err = tsl2563_detect(chip);
  605. if (err)
  606. return dev_err_probe(dev, err, "detect error\n");
  607. err = tsl2563_read_id(chip, &id);
  608. if (err)
  609. return dev_err_probe(dev, err, "read id error\n");
  610. mutex_init(&chip->lock);
  611. /* Default values used until userspace says otherwise */
  612. chip->low_thres = 0x0;
  613. chip->high_thres = 0xffff;
  614. chip->gainlevel = tsl2563_gainlevel_table;
  615. chip->intr = TSL2563_INT_PERSIST(4);
  616. chip->calib0 = tsl2563_calib_from_sysfs(CALIB_BASE_SYSFS);
  617. chip->calib1 = tsl2563_calib_from_sysfs(CALIB_BASE_SYSFS);
  618. chip->cover_comp_gain = 1;
  619. device_property_read_u32(dev, "amstaos,cover-comp-gain", &chip->cover_comp_gain);
  620. dev_info(dev, "model %d, rev. %d\n", id >> 4, id & 0x0f);
  621. indio_dev->name = client->name;
  622. indio_dev->channels = tsl2563_channels;
  623. indio_dev->num_channels = ARRAY_SIZE(tsl2563_channels);
  624. indio_dev->modes = INDIO_DIRECT_MODE;
  625. if (client->irq)
  626. indio_dev->info = &tsl2563_info;
  627. else
  628. indio_dev->info = &tsl2563_info_no_irq;
  629. if (client->irq) {
  630. irq_flags = irq_get_trigger_type(client->irq);
  631. if (irq_flags == IRQF_TRIGGER_NONE)
  632. irq_flags = IRQF_TRIGGER_RISING;
  633. irq_flags |= IRQF_ONESHOT;
  634. err = devm_request_threaded_irq(dev, client->irq,
  635. NULL,
  636. &tsl2563_event_handler,
  637. irq_flags,
  638. "tsl2563_event",
  639. indio_dev);
  640. if (err)
  641. return dev_err_probe(dev, err, "irq request error\n");
  642. }
  643. err = tsl2563_configure(chip);
  644. if (err)
  645. return dev_err_probe(dev, err, "configure error\n");
  646. INIT_DELAYED_WORK(&chip->poweroff_work, tsl2563_poweroff_work);
  647. /* The interrupt cannot yet be enabled so this is fine without lock */
  648. schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
  649. err = iio_device_register(indio_dev);
  650. if (err) {
  651. dev_err_probe(dev, err, "iio registration error\n");
  652. goto fail;
  653. }
  654. return 0;
  655. fail:
  656. cancel_delayed_work_sync(&chip->poweroff_work);
  657. return err;
  658. }
  659. static void tsl2563_remove(struct i2c_client *client)
  660. {
  661. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  662. struct tsl2563_chip *chip = iio_priv(indio_dev);
  663. iio_device_unregister(indio_dev);
  664. if (!chip->int_enabled)
  665. cancel_delayed_work_sync(&chip->poweroff_work);
  666. /* Ensure that interrupts are disabled - then flush any bottom halves */
  667. tsl2563_configure_irq(chip, false);
  668. tsl2563_set_power(chip, 0);
  669. }
  670. static int tsl2563_suspend(struct device *dev)
  671. {
  672. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  673. struct tsl2563_chip *chip = iio_priv(indio_dev);
  674. int ret;
  675. mutex_lock(&chip->lock);
  676. ret = tsl2563_set_power(chip, 0);
  677. if (ret)
  678. goto out;
  679. chip->suspended = true;
  680. out:
  681. mutex_unlock(&chip->lock);
  682. return ret;
  683. }
  684. static int tsl2563_resume(struct device *dev)
  685. {
  686. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  687. struct tsl2563_chip *chip = iio_priv(indio_dev);
  688. int ret;
  689. mutex_lock(&chip->lock);
  690. ret = tsl2563_set_power(chip, 1);
  691. if (ret)
  692. goto out;
  693. ret = tsl2563_configure(chip);
  694. if (ret)
  695. goto out;
  696. chip->suspended = false;
  697. out:
  698. mutex_unlock(&chip->lock);
  699. return ret;
  700. }
  701. static DEFINE_SIMPLE_DEV_PM_OPS(tsl2563_pm_ops, tsl2563_suspend,
  702. tsl2563_resume);
  703. static const struct i2c_device_id tsl2563_id[] = {
  704. { "tsl2560", 0 },
  705. { "tsl2561", 1 },
  706. { "tsl2562", 2 },
  707. { "tsl2563", 3 },
  708. {}
  709. };
  710. MODULE_DEVICE_TABLE(i2c, tsl2563_id);
  711. static const struct of_device_id tsl2563_of_match[] = {
  712. { .compatible = "amstaos,tsl2560" },
  713. { .compatible = "amstaos,tsl2561" },
  714. { .compatible = "amstaos,tsl2562" },
  715. { .compatible = "amstaos,tsl2563" },
  716. {}
  717. };
  718. MODULE_DEVICE_TABLE(of, tsl2563_of_match);
  719. static struct i2c_driver tsl2563_i2c_driver = {
  720. .driver = {
  721. .name = "tsl2563",
  722. .of_match_table = tsl2563_of_match,
  723. .pm = pm_sleep_ptr(&tsl2563_pm_ops),
  724. },
  725. .probe = tsl2563_probe,
  726. .remove = tsl2563_remove,
  727. .id_table = tsl2563_id,
  728. };
  729. module_i2c_driver(tsl2563_i2c_driver);
  730. MODULE_AUTHOR("Nokia Corporation");
  731. MODULE_DESCRIPTION("tsl2563 light sensor driver");
  732. MODULE_LICENSE("GPL");