tsl2563.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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 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 normalize_adc(u16 adc, u8 timing)
  228. {
  229. return adc << 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 = normalize_adc(adc0, chip->gainlevel->gaintime);
  299. chip->data1 = 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 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 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 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 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 (chan->channel == IIO_MOD_LIGHT_BOTH)
  392. chip->calib0 = calib_from_sysfs(val);
  393. else
  394. chip->calib1 = calib_from_sysfs(val);
  395. return 0;
  396. }
  397. static int tsl2563_read_raw(struct iio_dev *indio_dev,
  398. struct iio_chan_spec const *chan,
  399. int *val,
  400. int *val2,
  401. long m)
  402. {
  403. int ret = -EINVAL;
  404. u32 calib0, calib1;
  405. struct tsl2563_chip *chip = iio_priv(indio_dev);
  406. mutex_lock(&chip->lock);
  407. switch (m) {
  408. case IIO_CHAN_INFO_RAW:
  409. case IIO_CHAN_INFO_PROCESSED:
  410. switch (chan->type) {
  411. case IIO_LIGHT:
  412. ret = tsl2563_get_adc(chip);
  413. if (ret)
  414. goto error_ret;
  415. calib0 = calib_adc(chip->data0, chip->calib0) *
  416. chip->cover_comp_gain;
  417. calib1 = calib_adc(chip->data1, chip->calib1) *
  418. chip->cover_comp_gain;
  419. *val = adc_to_lux(calib0, calib1);
  420. ret = IIO_VAL_INT;
  421. break;
  422. case IIO_INTENSITY:
  423. ret = tsl2563_get_adc(chip);
  424. if (ret)
  425. goto error_ret;
  426. if (chan->channel == 0)
  427. *val = chip->data0;
  428. else
  429. *val = chip->data1;
  430. ret = IIO_VAL_INT;
  431. break;
  432. default:
  433. break;
  434. }
  435. break;
  436. case IIO_CHAN_INFO_CALIBSCALE:
  437. if (chan->channel == 0)
  438. *val = calib_to_sysfs(chip->calib0);
  439. else
  440. *val = calib_to_sysfs(chip->calib1);
  441. ret = IIO_VAL_INT;
  442. break;
  443. default:
  444. ret = -EINVAL;
  445. goto error_ret;
  446. }
  447. error_ret:
  448. mutex_unlock(&chip->lock);
  449. return ret;
  450. }
  451. static const struct iio_chan_spec tsl2563_channels[] = {
  452. {
  453. .type = IIO_LIGHT,
  454. .indexed = 1,
  455. .info_mask = IIO_CHAN_INFO_PROCESSED_SEPARATE_BIT,
  456. .channel = 0,
  457. }, {
  458. .type = IIO_INTENSITY,
  459. .modified = 1,
  460. .channel2 = IIO_MOD_LIGHT_BOTH,
  461. .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
  462. IIO_CHAN_INFO_CALIBSCALE_SEPARATE_BIT,
  463. .event_mask = (IIO_EV_BIT(IIO_EV_TYPE_THRESH,
  464. IIO_EV_DIR_RISING) |
  465. IIO_EV_BIT(IIO_EV_TYPE_THRESH,
  466. IIO_EV_DIR_FALLING)),
  467. }, {
  468. .type = IIO_INTENSITY,
  469. .modified = 1,
  470. .channel2 = IIO_MOD_LIGHT_IR,
  471. .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
  472. IIO_CHAN_INFO_CALIBSCALE_SEPARATE_BIT,
  473. }
  474. };
  475. static int tsl2563_read_thresh(struct iio_dev *indio_dev,
  476. u64 event_code,
  477. int *val)
  478. {
  479. struct tsl2563_chip *chip = iio_priv(indio_dev);
  480. switch (IIO_EVENT_CODE_EXTRACT_DIR(event_code)) {
  481. case IIO_EV_DIR_RISING:
  482. *val = chip->high_thres;
  483. break;
  484. case IIO_EV_DIR_FALLING:
  485. *val = chip->low_thres;
  486. break;
  487. default:
  488. return -EINVAL;
  489. }
  490. return 0;
  491. }
  492. static int tsl2563_write_thresh(struct iio_dev *indio_dev,
  493. u64 event_code,
  494. int val)
  495. {
  496. struct tsl2563_chip *chip = iio_priv(indio_dev);
  497. int ret;
  498. u8 address;
  499. if (IIO_EVENT_CODE_EXTRACT_DIR(event_code) == IIO_EV_DIR_RISING)
  500. address = TSL2563_REG_HIGHLOW;
  501. else
  502. address = TSL2563_REG_LOWLOW;
  503. mutex_lock(&chip->lock);
  504. ret = i2c_smbus_write_byte_data(chip->client, TSL2563_CMD | address,
  505. val & 0xFF);
  506. if (ret)
  507. goto error_ret;
  508. ret = i2c_smbus_write_byte_data(chip->client,
  509. TSL2563_CMD | (address + 1),
  510. (val >> 8) & 0xFF);
  511. if (IIO_EVENT_CODE_EXTRACT_DIR(event_code) == IIO_EV_DIR_RISING)
  512. chip->high_thres = val;
  513. else
  514. chip->low_thres = val;
  515. error_ret:
  516. mutex_unlock(&chip->lock);
  517. return ret;
  518. }
  519. static irqreturn_t tsl2563_event_handler(int irq, void *private)
  520. {
  521. struct iio_dev *dev_info = private;
  522. struct tsl2563_chip *chip = iio_priv(dev_info);
  523. iio_push_event(dev_info,
  524. IIO_UNMOD_EVENT_CODE(IIO_LIGHT,
  525. 0,
  526. IIO_EV_TYPE_THRESH,
  527. IIO_EV_DIR_EITHER),
  528. iio_get_time_ns());
  529. /* clear the interrupt and push the event */
  530. i2c_smbus_write_byte(chip->client, TSL2563_CMD | TSL2563_CLEARINT);
  531. return IRQ_HANDLED;
  532. }
  533. static int tsl2563_write_interrupt_config(struct iio_dev *indio_dev,
  534. u64 event_code,
  535. int state)
  536. {
  537. struct tsl2563_chip *chip = iio_priv(indio_dev);
  538. int ret = 0;
  539. mutex_lock(&chip->lock);
  540. if (state && !(chip->intr & 0x30)) {
  541. chip->intr &= ~0x30;
  542. chip->intr |= 0x10;
  543. /* ensure the chip is actually on */
  544. cancel_delayed_work(&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 = i2c_smbus_write_byte_data(chip->client,
  554. TSL2563_CMD | TSL2563_REG_INT,
  555. chip->intr);
  556. chip->int_enabled = true;
  557. }
  558. if (!state && (chip->intr & 0x30)) {
  559. chip->intr &= ~0x30;
  560. ret = i2c_smbus_write_byte_data(chip->client,
  561. TSL2563_CMD | TSL2563_REG_INT,
  562. chip->intr);
  563. chip->int_enabled = false;
  564. /* now the interrupt is not enabled, we can go to sleep */
  565. schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
  566. }
  567. out:
  568. mutex_unlock(&chip->lock);
  569. return ret;
  570. }
  571. static int tsl2563_read_interrupt_config(struct iio_dev *indio_dev,
  572. u64 event_code)
  573. {
  574. struct tsl2563_chip *chip = iio_priv(indio_dev);
  575. int ret;
  576. mutex_lock(&chip->lock);
  577. ret = i2c_smbus_read_byte_data(chip->client,
  578. TSL2563_CMD | TSL2563_REG_INT);
  579. mutex_unlock(&chip->lock);
  580. if (ret < 0)
  581. return ret;
  582. return !!(ret & 0x30);
  583. }
  584. static const struct iio_info tsl2563_info_no_irq = {
  585. .driver_module = THIS_MODULE,
  586. .read_raw = &tsl2563_read_raw,
  587. .write_raw = &tsl2563_write_raw,
  588. };
  589. static const struct iio_info tsl2563_info = {
  590. .driver_module = THIS_MODULE,
  591. .read_raw = &tsl2563_read_raw,
  592. .write_raw = &tsl2563_write_raw,
  593. .read_event_value = &tsl2563_read_thresh,
  594. .write_event_value = &tsl2563_write_thresh,
  595. .read_event_config = &tsl2563_read_interrupt_config,
  596. .write_event_config = &tsl2563_write_interrupt_config,
  597. };
  598. static int tsl2563_probe(struct i2c_client *client,
  599. const struct i2c_device_id *device_id)
  600. {
  601. struct iio_dev *indio_dev;
  602. struct tsl2563_chip *chip;
  603. struct tsl2563_platform_data *pdata = client->dev.platform_data;
  604. int err = 0;
  605. u8 id = 0;
  606. indio_dev = iio_device_alloc(sizeof(*chip));
  607. if (!indio_dev)
  608. return -ENOMEM;
  609. chip = iio_priv(indio_dev);
  610. i2c_set_clientdata(client, chip);
  611. chip->client = client;
  612. err = tsl2563_detect(chip);
  613. if (err) {
  614. dev_err(&client->dev, "detect error %d\n", -err);
  615. goto fail1;
  616. }
  617. err = tsl2563_read_id(chip, &id);
  618. if (err) {
  619. dev_err(&client->dev, "read id error %d\n", -err);
  620. goto fail1;
  621. }
  622. mutex_init(&chip->lock);
  623. /* Default values used until userspace says otherwise */
  624. chip->low_thres = 0x0;
  625. chip->high_thres = 0xffff;
  626. chip->gainlevel = tsl2563_gainlevel_table;
  627. chip->intr = TSL2563_INT_PERSIST(4);
  628. chip->calib0 = calib_from_sysfs(CALIB_BASE_SYSFS);
  629. chip->calib1 = calib_from_sysfs(CALIB_BASE_SYSFS);
  630. if (pdata)
  631. chip->cover_comp_gain = pdata->cover_comp_gain;
  632. else
  633. chip->cover_comp_gain = 1;
  634. dev_info(&client->dev, "model %d, rev. %d\n", id >> 4, id & 0x0f);
  635. indio_dev->name = client->name;
  636. indio_dev->channels = tsl2563_channels;
  637. indio_dev->num_channels = ARRAY_SIZE(tsl2563_channels);
  638. indio_dev->dev.parent = &client->dev;
  639. indio_dev->modes = INDIO_DIRECT_MODE;
  640. if (client->irq)
  641. indio_dev->info = &tsl2563_info;
  642. else
  643. indio_dev->info = &tsl2563_info_no_irq;
  644. if (client->irq) {
  645. err = request_threaded_irq(client->irq,
  646. NULL,
  647. &tsl2563_event_handler,
  648. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  649. "tsl2563_event",
  650. indio_dev);
  651. if (err) {
  652. dev_err(&client->dev, "irq request error %d\n", -err);
  653. goto fail1;
  654. }
  655. }
  656. err = tsl2563_configure(chip);
  657. if (err) {
  658. dev_err(&client->dev, "configure error %d\n", -err);
  659. goto fail2;
  660. }
  661. INIT_DELAYED_WORK(&chip->poweroff_work, tsl2563_poweroff_work);
  662. /* The interrupt cannot yet be enabled so this is fine without lock */
  663. schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
  664. err = iio_device_register(indio_dev);
  665. if (err) {
  666. dev_err(&client->dev, "iio registration error %d\n", -err);
  667. goto fail3;
  668. }
  669. return 0;
  670. fail3:
  671. cancel_delayed_work(&chip->poweroff_work);
  672. flush_scheduled_work();
  673. fail2:
  674. if (client->irq)
  675. free_irq(client->irq, indio_dev);
  676. fail1:
  677. iio_device_free(indio_dev);
  678. return err;
  679. }
  680. static int tsl2563_remove(struct i2c_client *client)
  681. {
  682. struct tsl2563_chip *chip = i2c_get_clientdata(client);
  683. struct iio_dev *indio_dev = iio_priv_to_dev(chip);
  684. iio_device_unregister(indio_dev);
  685. if (!chip->int_enabled)
  686. cancel_delayed_work(&chip->poweroff_work);
  687. /* Ensure that interrupts are disabled - then flush any bottom halves */
  688. chip->intr &= ~0x30;
  689. i2c_smbus_write_byte_data(chip->client, TSL2563_CMD | TSL2563_REG_INT,
  690. chip->intr);
  691. flush_scheduled_work();
  692. tsl2563_set_power(chip, 0);
  693. if (client->irq)
  694. free_irq(client->irq, indio_dev);
  695. iio_device_free(indio_dev);
  696. return 0;
  697. }
  698. #ifdef CONFIG_PM_SLEEP
  699. static int tsl2563_suspend(struct device *dev)
  700. {
  701. struct tsl2563_chip *chip = i2c_get_clientdata(to_i2c_client(dev));
  702. int ret;
  703. mutex_lock(&chip->lock);
  704. ret = tsl2563_set_power(chip, 0);
  705. if (ret)
  706. goto out;
  707. chip->suspended = true;
  708. out:
  709. mutex_unlock(&chip->lock);
  710. return ret;
  711. }
  712. static int tsl2563_resume(struct device *dev)
  713. {
  714. struct tsl2563_chip *chip = i2c_get_clientdata(to_i2c_client(dev));
  715. int ret;
  716. mutex_lock(&chip->lock);
  717. ret = tsl2563_set_power(chip, 1);
  718. if (ret)
  719. goto out;
  720. ret = tsl2563_configure(chip);
  721. if (ret)
  722. goto out;
  723. chip->suspended = false;
  724. out:
  725. mutex_unlock(&chip->lock);
  726. return ret;
  727. }
  728. static SIMPLE_DEV_PM_OPS(tsl2563_pm_ops, tsl2563_suspend, tsl2563_resume);
  729. #define TSL2563_PM_OPS (&tsl2563_pm_ops)
  730. #else
  731. #define TSL2563_PM_OPS NULL
  732. #endif
  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 struct i2c_driver tsl2563_i2c_driver = {
  742. .driver = {
  743. .name = "tsl2563",
  744. .pm = TSL2563_PM_OPS,
  745. },
  746. .probe = tsl2563_probe,
  747. .remove = tsl2563_remove,
  748. .id_table = tsl2563_id,
  749. };
  750. module_i2c_driver(tsl2563_i2c_driver);
  751. MODULE_AUTHOR("Nokia Corporation");
  752. MODULE_DESCRIPTION("tsl2563 light sensor driver");
  753. MODULE_LICENSE("GPL");