apds9300.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /*
  2. * apds9300.c - IIO driver for Avago APDS9300 ambient light sensor
  3. *
  4. * Copyright 2013 Oleksandr Kravchenko <o.v.kravchenko@globallogic.com>
  5. *
  6. * This file is subject to the terms and conditions of version 2 of
  7. * the GNU General Public License. See the file COPYING in the main
  8. * directory of this archive for more details.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/slab.h>
  12. #include <linux/pm.h>
  13. #include <linux/i2c.h>
  14. #include <linux/err.h>
  15. #include <linux/mutex.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/iio/iio.h>
  18. #include <linux/iio/sysfs.h>
  19. #include <linux/iio/events.h>
  20. #define APDS9300_DRV_NAME "apds9300"
  21. #define APDS9300_IRQ_NAME "apds9300_event"
  22. /* Command register bits */
  23. #define APDS9300_CMD BIT(7) /* Select command register. Must write as 1 */
  24. #define APDS9300_WORD BIT(5) /* I2C write/read: if 1 word, if 0 byte */
  25. #define APDS9300_CLEAR BIT(6) /* Interrupt clear. Clears pending interrupt */
  26. /* Register set */
  27. #define APDS9300_CONTROL 0x00 /* Control of basic functions */
  28. #define APDS9300_THRESHLOWLOW 0x02 /* Low byte of low interrupt threshold */
  29. #define APDS9300_THRESHHIGHLOW 0x04 /* Low byte of high interrupt threshold */
  30. #define APDS9300_INTERRUPT 0x06 /* Interrupt control */
  31. #define APDS9300_DATA0LOW 0x0c /* Low byte of ADC channel 0 */
  32. #define APDS9300_DATA1LOW 0x0e /* Low byte of ADC channel 1 */
  33. /* Power on/off value for APDS9300_CONTROL register */
  34. #define APDS9300_POWER_ON 0x03
  35. #define APDS9300_POWER_OFF 0x00
  36. /* Interrupts */
  37. #define APDS9300_INTR_ENABLE 0x10
  38. /* Interrupt Persist Function: Any value outside of threshold range */
  39. #define APDS9300_THRESH_INTR 0x01
  40. #define APDS9300_THRESH_MAX 0xffff /* Max threshold value */
  41. struct apds9300_data {
  42. struct i2c_client *client;
  43. struct mutex mutex;
  44. int power_state;
  45. int thresh_low;
  46. int thresh_hi;
  47. int intr_en;
  48. };
  49. /* Lux calculation */
  50. /* Calculated values 1000 * (CH1/CH0)^1.4 for CH1/CH0 from 0 to 0.52 */
  51. static const u16 apds9300_lux_ratio[] = {
  52. 0, 2, 4, 7, 11, 15, 19, 24, 29, 34, 40, 45, 51, 57, 64, 70, 77, 84, 91,
  53. 98, 105, 112, 120, 128, 136, 144, 152, 160, 168, 177, 185, 194, 203,
  54. 212, 221, 230, 239, 249, 258, 268, 277, 287, 297, 307, 317, 327, 337,
  55. 347, 358, 368, 379, 390, 400,
  56. };
  57. static unsigned long apds9300_calculate_lux(u16 ch0, u16 ch1)
  58. {
  59. unsigned long lux, tmp;
  60. /* avoid division by zero */
  61. if (ch0 == 0)
  62. return 0;
  63. tmp = DIV_ROUND_UP(ch1 * 100, ch0);
  64. if (tmp <= 52) {
  65. lux = 3150 * ch0 - (unsigned long)DIV_ROUND_UP_ULL(ch0
  66. * apds9300_lux_ratio[tmp] * 5930ull, 1000);
  67. } else if (tmp <= 65) {
  68. lux = 2290 * ch0 - 2910 * ch1;
  69. } else if (tmp <= 80) {
  70. lux = 1570 * ch0 - 1800 * ch1;
  71. } else if (tmp <= 130) {
  72. lux = 338 * ch0 - 260 * ch1;
  73. } else {
  74. lux = 0;
  75. }
  76. return lux / 100000;
  77. }
  78. static int apds9300_get_adc_val(struct apds9300_data *data, int adc_number)
  79. {
  80. int ret;
  81. u8 flags = APDS9300_CMD | APDS9300_WORD;
  82. if (!data->power_state)
  83. return -EBUSY;
  84. /* Select ADC0 or ADC1 data register */
  85. flags |= adc_number ? APDS9300_DATA1LOW : APDS9300_DATA0LOW;
  86. ret = i2c_smbus_read_word_data(data->client, flags);
  87. if (ret < 0)
  88. dev_err(&data->client->dev,
  89. "failed to read ADC%d value\n", adc_number);
  90. return ret;
  91. }
  92. static int apds9300_set_thresh_low(struct apds9300_data *data, int value)
  93. {
  94. int ret;
  95. if (!data->power_state)
  96. return -EBUSY;
  97. if (value > APDS9300_THRESH_MAX)
  98. return -EINVAL;
  99. ret = i2c_smbus_write_word_data(data->client, APDS9300_THRESHLOWLOW
  100. | APDS9300_CMD | APDS9300_WORD, value);
  101. if (ret) {
  102. dev_err(&data->client->dev, "failed to set thresh_low\n");
  103. return ret;
  104. }
  105. data->thresh_low = value;
  106. return 0;
  107. }
  108. static int apds9300_set_thresh_hi(struct apds9300_data *data, int value)
  109. {
  110. int ret;
  111. if (!data->power_state)
  112. return -EBUSY;
  113. if (value > APDS9300_THRESH_MAX)
  114. return -EINVAL;
  115. ret = i2c_smbus_write_word_data(data->client, APDS9300_THRESHHIGHLOW
  116. | APDS9300_CMD | APDS9300_WORD, value);
  117. if (ret) {
  118. dev_err(&data->client->dev, "failed to set thresh_hi\n");
  119. return ret;
  120. }
  121. data->thresh_hi = value;
  122. return 0;
  123. }
  124. static int apds9300_set_intr_state(struct apds9300_data *data, int state)
  125. {
  126. int ret;
  127. u8 cmd;
  128. if (!data->power_state)
  129. return -EBUSY;
  130. cmd = state ? APDS9300_INTR_ENABLE | APDS9300_THRESH_INTR : 0x00;
  131. ret = i2c_smbus_write_byte_data(data->client,
  132. APDS9300_INTERRUPT | APDS9300_CMD, cmd);
  133. if (ret) {
  134. dev_err(&data->client->dev,
  135. "failed to set interrupt state %d\n", state);
  136. return ret;
  137. }
  138. data->intr_en = state;
  139. return 0;
  140. }
  141. static int apds9300_set_power_state(struct apds9300_data *data, int state)
  142. {
  143. int ret;
  144. u8 cmd;
  145. cmd = state ? APDS9300_POWER_ON : APDS9300_POWER_OFF;
  146. ret = i2c_smbus_write_byte_data(data->client,
  147. APDS9300_CONTROL | APDS9300_CMD, cmd);
  148. if (ret) {
  149. dev_err(&data->client->dev,
  150. "failed to set power state %d\n", state);
  151. return ret;
  152. }
  153. data->power_state = state;
  154. return 0;
  155. }
  156. static void apds9300_clear_intr(struct apds9300_data *data)
  157. {
  158. int ret;
  159. ret = i2c_smbus_write_byte(data->client, APDS9300_CLEAR | APDS9300_CMD);
  160. if (ret < 0)
  161. dev_err(&data->client->dev, "failed to clear interrupt\n");
  162. }
  163. static int apds9300_chip_init(struct apds9300_data *data)
  164. {
  165. int ret;
  166. /* Need to set power off to ensure that the chip is off */
  167. ret = apds9300_set_power_state(data, 0);
  168. if (ret < 0)
  169. goto err;
  170. /*
  171. * Probe the chip. To do so we try to power up the device and then to
  172. * read back the 0x03 code
  173. */
  174. ret = apds9300_set_power_state(data, 1);
  175. if (ret < 0)
  176. goto err;
  177. ret = i2c_smbus_read_byte_data(data->client,
  178. APDS9300_CONTROL | APDS9300_CMD);
  179. if (ret != APDS9300_POWER_ON) {
  180. ret = -ENODEV;
  181. goto err;
  182. }
  183. /*
  184. * Disable interrupt to ensure thai it is doesn't enable
  185. * i.e. after device soft reset
  186. */
  187. ret = apds9300_set_intr_state(data, 0);
  188. if (ret < 0)
  189. goto err;
  190. return 0;
  191. err:
  192. dev_err(&data->client->dev, "failed to init the chip\n");
  193. return ret;
  194. }
  195. static int apds9300_read_raw(struct iio_dev *indio_dev,
  196. struct iio_chan_spec const *chan, int *val, int *val2,
  197. long mask)
  198. {
  199. int ch0, ch1, ret = -EINVAL;
  200. struct apds9300_data *data = iio_priv(indio_dev);
  201. mutex_lock(&data->mutex);
  202. switch (chan->type) {
  203. case IIO_LIGHT:
  204. ch0 = apds9300_get_adc_val(data, 0);
  205. if (ch0 < 0) {
  206. ret = ch0;
  207. break;
  208. }
  209. ch1 = apds9300_get_adc_val(data, 1);
  210. if (ch1 < 0) {
  211. ret = ch1;
  212. break;
  213. }
  214. *val = apds9300_calculate_lux(ch0, ch1);
  215. ret = IIO_VAL_INT;
  216. break;
  217. case IIO_INTENSITY:
  218. ret = apds9300_get_adc_val(data, chan->channel);
  219. if (ret < 0)
  220. break;
  221. *val = ret;
  222. ret = IIO_VAL_INT;
  223. break;
  224. default:
  225. break;
  226. }
  227. mutex_unlock(&data->mutex);
  228. return ret;
  229. }
  230. static int apds9300_read_thresh(struct iio_dev *indio_dev,
  231. const struct iio_chan_spec *chan, enum iio_event_type type,
  232. enum iio_event_direction dir, enum iio_event_info info,
  233. int *val, int *val2)
  234. {
  235. struct apds9300_data *data = iio_priv(indio_dev);
  236. switch (dir) {
  237. case IIO_EV_DIR_RISING:
  238. *val = data->thresh_hi;
  239. break;
  240. case IIO_EV_DIR_FALLING:
  241. *val = data->thresh_low;
  242. break;
  243. default:
  244. return -EINVAL;
  245. }
  246. return IIO_VAL_INT;
  247. }
  248. static int apds9300_write_thresh(struct iio_dev *indio_dev,
  249. const struct iio_chan_spec *chan, enum iio_event_type type,
  250. enum iio_event_direction dir, enum iio_event_info info, int val,
  251. int val2)
  252. {
  253. struct apds9300_data *data = iio_priv(indio_dev);
  254. int ret;
  255. mutex_lock(&data->mutex);
  256. if (dir == IIO_EV_DIR_RISING)
  257. ret = apds9300_set_thresh_hi(data, val);
  258. else
  259. ret = apds9300_set_thresh_low(data, val);
  260. mutex_unlock(&data->mutex);
  261. return ret;
  262. }
  263. static int apds9300_read_interrupt_config(struct iio_dev *indio_dev,
  264. const struct iio_chan_spec *chan,
  265. enum iio_event_type type,
  266. enum iio_event_direction dir)
  267. {
  268. struct apds9300_data *data = iio_priv(indio_dev);
  269. return data->intr_en;
  270. }
  271. static int apds9300_write_interrupt_config(struct iio_dev *indio_dev,
  272. const struct iio_chan_spec *chan, enum iio_event_type type,
  273. enum iio_event_direction dir, int state)
  274. {
  275. struct apds9300_data *data = iio_priv(indio_dev);
  276. int ret;
  277. mutex_lock(&data->mutex);
  278. ret = apds9300_set_intr_state(data, state);
  279. mutex_unlock(&data->mutex);
  280. return ret;
  281. }
  282. static const struct iio_info apds9300_info_no_irq = {
  283. .read_raw = apds9300_read_raw,
  284. };
  285. static const struct iio_info apds9300_info = {
  286. .read_raw = apds9300_read_raw,
  287. .read_event_value = apds9300_read_thresh,
  288. .write_event_value = apds9300_write_thresh,
  289. .read_event_config = apds9300_read_interrupt_config,
  290. .write_event_config = apds9300_write_interrupt_config,
  291. };
  292. static const struct iio_event_spec apds9300_event_spec[] = {
  293. {
  294. .type = IIO_EV_TYPE_THRESH,
  295. .dir = IIO_EV_DIR_RISING,
  296. .mask_separate = BIT(IIO_EV_INFO_VALUE) |
  297. BIT(IIO_EV_INFO_ENABLE),
  298. }, {
  299. .type = IIO_EV_TYPE_THRESH,
  300. .dir = IIO_EV_DIR_FALLING,
  301. .mask_separate = BIT(IIO_EV_INFO_VALUE) |
  302. BIT(IIO_EV_INFO_ENABLE),
  303. },
  304. };
  305. static const struct iio_chan_spec apds9300_channels[] = {
  306. {
  307. .type = IIO_LIGHT,
  308. .channel = 0,
  309. .indexed = true,
  310. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
  311. }, {
  312. .type = IIO_INTENSITY,
  313. .channel = 0,
  314. .channel2 = IIO_MOD_LIGHT_BOTH,
  315. .indexed = true,
  316. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  317. .event_spec = apds9300_event_spec,
  318. .num_event_specs = ARRAY_SIZE(apds9300_event_spec),
  319. }, {
  320. .type = IIO_INTENSITY,
  321. .channel = 1,
  322. .channel2 = IIO_MOD_LIGHT_IR,
  323. .indexed = true,
  324. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  325. },
  326. };
  327. static irqreturn_t apds9300_interrupt_handler(int irq, void *private)
  328. {
  329. struct iio_dev *dev_info = private;
  330. struct apds9300_data *data = iio_priv(dev_info);
  331. iio_push_event(dev_info,
  332. IIO_UNMOD_EVENT_CODE(IIO_INTENSITY, 0,
  333. IIO_EV_TYPE_THRESH,
  334. IIO_EV_DIR_EITHER),
  335. iio_get_time_ns(dev_info));
  336. apds9300_clear_intr(data);
  337. return IRQ_HANDLED;
  338. }
  339. static int apds9300_probe(struct i2c_client *client,
  340. const struct i2c_device_id *id)
  341. {
  342. struct apds9300_data *data;
  343. struct iio_dev *indio_dev;
  344. int ret;
  345. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  346. if (!indio_dev)
  347. return -ENOMEM;
  348. data = iio_priv(indio_dev);
  349. i2c_set_clientdata(client, indio_dev);
  350. data->client = client;
  351. ret = apds9300_chip_init(data);
  352. if (ret < 0)
  353. goto err;
  354. mutex_init(&data->mutex);
  355. indio_dev->dev.parent = &client->dev;
  356. indio_dev->channels = apds9300_channels;
  357. indio_dev->num_channels = ARRAY_SIZE(apds9300_channels);
  358. indio_dev->name = APDS9300_DRV_NAME;
  359. indio_dev->modes = INDIO_DIRECT_MODE;
  360. if (client->irq)
  361. indio_dev->info = &apds9300_info;
  362. else
  363. indio_dev->info = &apds9300_info_no_irq;
  364. if (client->irq) {
  365. ret = devm_request_threaded_irq(&client->dev, client->irq,
  366. NULL, apds9300_interrupt_handler,
  367. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  368. APDS9300_IRQ_NAME, indio_dev);
  369. if (ret) {
  370. dev_err(&client->dev, "irq request error %d\n", -ret);
  371. goto err;
  372. }
  373. }
  374. ret = iio_device_register(indio_dev);
  375. if (ret < 0)
  376. goto err;
  377. return 0;
  378. err:
  379. /* Ensure that power off in case of error */
  380. apds9300_set_power_state(data, 0);
  381. return ret;
  382. }
  383. static int apds9300_remove(struct i2c_client *client)
  384. {
  385. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  386. struct apds9300_data *data = iio_priv(indio_dev);
  387. iio_device_unregister(indio_dev);
  388. /* Ensure that power off and interrupts are disabled */
  389. apds9300_set_intr_state(data, 0);
  390. apds9300_set_power_state(data, 0);
  391. return 0;
  392. }
  393. #ifdef CONFIG_PM_SLEEP
  394. static int apds9300_suspend(struct device *dev)
  395. {
  396. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  397. struct apds9300_data *data = iio_priv(indio_dev);
  398. int ret;
  399. mutex_lock(&data->mutex);
  400. ret = apds9300_set_power_state(data, 0);
  401. mutex_unlock(&data->mutex);
  402. return ret;
  403. }
  404. static int apds9300_resume(struct device *dev)
  405. {
  406. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  407. struct apds9300_data *data = iio_priv(indio_dev);
  408. int ret;
  409. mutex_lock(&data->mutex);
  410. ret = apds9300_set_power_state(data, 1);
  411. mutex_unlock(&data->mutex);
  412. return ret;
  413. }
  414. static SIMPLE_DEV_PM_OPS(apds9300_pm_ops, apds9300_suspend, apds9300_resume);
  415. #define APDS9300_PM_OPS (&apds9300_pm_ops)
  416. #else
  417. #define APDS9300_PM_OPS NULL
  418. #endif
  419. static const struct i2c_device_id apds9300_id[] = {
  420. { APDS9300_DRV_NAME, 0 },
  421. { }
  422. };
  423. MODULE_DEVICE_TABLE(i2c, apds9300_id);
  424. static struct i2c_driver apds9300_driver = {
  425. .driver = {
  426. .name = APDS9300_DRV_NAME,
  427. .pm = APDS9300_PM_OPS,
  428. },
  429. .probe = apds9300_probe,
  430. .remove = apds9300_remove,
  431. .id_table = apds9300_id,
  432. };
  433. module_i2c_driver(apds9300_driver);
  434. MODULE_AUTHOR("Kravchenko Oleksandr <o.v.kravchenko@globallogic.com>");
  435. MODULE_AUTHOR("GlobalLogic inc.");
  436. MODULE_DESCRIPTION("APDS9300 ambient light photo sensor driver");
  437. MODULE_LICENSE("GPL");