tsl2563.c 21 KB

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