opt3001.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * opt3001.c - Texas Instruments OPT3001 Light Sensor
  4. *
  5. * Copyright (C) 2014 Texas Instruments Incorporated - https://www.ti.com
  6. *
  7. * Author: Andreas Dannenberg <[email protected]>
  8. * Based on previous work from: Felipe Balbi <[email protected]>
  9. */
  10. #include <linux/bitops.h>
  11. #include <linux/delay.h>
  12. #include <linux/device.h>
  13. #include <linux/i2c.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/irq.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/mod_devicetable.h>
  19. #include <linux/mutex.h>
  20. #include <linux/slab.h>
  21. #include <linux/types.h>
  22. #include <linux/iio/events.h>
  23. #include <linux/iio/iio.h>
  24. #include <linux/iio/sysfs.h>
  25. #define OPT3001_RESULT 0x00
  26. #define OPT3001_CONFIGURATION 0x01
  27. #define OPT3001_LOW_LIMIT 0x02
  28. #define OPT3001_HIGH_LIMIT 0x03
  29. #define OPT3001_MANUFACTURER_ID 0x7e
  30. #define OPT3001_DEVICE_ID 0x7f
  31. #define OPT3001_CONFIGURATION_RN_MASK (0xf << 12)
  32. #define OPT3001_CONFIGURATION_RN_AUTO (0xc << 12)
  33. #define OPT3001_CONFIGURATION_CT BIT(11)
  34. #define OPT3001_CONFIGURATION_M_MASK (3 << 9)
  35. #define OPT3001_CONFIGURATION_M_SHUTDOWN (0 << 9)
  36. #define OPT3001_CONFIGURATION_M_SINGLE (1 << 9)
  37. #define OPT3001_CONFIGURATION_M_CONTINUOUS (2 << 9) /* also 3 << 9 */
  38. #define OPT3001_CONFIGURATION_OVF BIT(8)
  39. #define OPT3001_CONFIGURATION_CRF BIT(7)
  40. #define OPT3001_CONFIGURATION_FH BIT(6)
  41. #define OPT3001_CONFIGURATION_FL BIT(5)
  42. #define OPT3001_CONFIGURATION_L BIT(4)
  43. #define OPT3001_CONFIGURATION_POL BIT(3)
  44. #define OPT3001_CONFIGURATION_ME BIT(2)
  45. #define OPT3001_CONFIGURATION_FC_MASK (3 << 0)
  46. /* The end-of-conversion enable is located in the low-limit register */
  47. #define OPT3001_LOW_LIMIT_EOC_ENABLE 0xc000
  48. #define OPT3001_REG_EXPONENT(n) ((n) >> 12)
  49. #define OPT3001_REG_MANTISSA(n) ((n) & 0xfff)
  50. #define OPT3001_INT_TIME_LONG 800000
  51. #define OPT3001_INT_TIME_SHORT 100000
  52. /*
  53. * Time to wait for conversion result to be ready. The device datasheet
  54. * sect. 6.5 states results are ready after total integration time plus 3ms.
  55. * This results in worst-case max values of 113ms or 883ms, respectively.
  56. * Add some slack to be on the safe side.
  57. */
  58. #define OPT3001_RESULT_READY_SHORT 150
  59. #define OPT3001_RESULT_READY_LONG 1000
  60. struct opt3001 {
  61. struct i2c_client *client;
  62. struct device *dev;
  63. struct mutex lock;
  64. bool ok_to_ignore_lock;
  65. bool result_ready;
  66. wait_queue_head_t result_ready_queue;
  67. u16 result;
  68. u32 int_time;
  69. u32 mode;
  70. u16 high_thresh_mantissa;
  71. u16 low_thresh_mantissa;
  72. u8 high_thresh_exp;
  73. u8 low_thresh_exp;
  74. bool use_irq;
  75. };
  76. struct opt3001_scale {
  77. int val;
  78. int val2;
  79. };
  80. static const struct opt3001_scale opt3001_scales[] = {
  81. {
  82. .val = 40,
  83. .val2 = 950000,
  84. },
  85. {
  86. .val = 81,
  87. .val2 = 900000,
  88. },
  89. {
  90. .val = 163,
  91. .val2 = 800000,
  92. },
  93. {
  94. .val = 327,
  95. .val2 = 600000,
  96. },
  97. {
  98. .val = 655,
  99. .val2 = 200000,
  100. },
  101. {
  102. .val = 1310,
  103. .val2 = 400000,
  104. },
  105. {
  106. .val = 2620,
  107. .val2 = 800000,
  108. },
  109. {
  110. .val = 5241,
  111. .val2 = 600000,
  112. },
  113. {
  114. .val = 10483,
  115. .val2 = 200000,
  116. },
  117. {
  118. .val = 20966,
  119. .val2 = 400000,
  120. },
  121. {
  122. .val = 83865,
  123. .val2 = 600000,
  124. },
  125. };
  126. static int opt3001_find_scale(const struct opt3001 *opt, int val,
  127. int val2, u8 *exponent)
  128. {
  129. int i;
  130. for (i = 0; i < ARRAY_SIZE(opt3001_scales); i++) {
  131. const struct opt3001_scale *scale = &opt3001_scales[i];
  132. /*
  133. * Combine the integer and micro parts for comparison
  134. * purposes. Use milli lux precision to avoid 32-bit integer
  135. * overflows.
  136. */
  137. if ((val * 1000 + val2 / 1000) <=
  138. (scale->val * 1000 + scale->val2 / 1000)) {
  139. *exponent = i;
  140. return 0;
  141. }
  142. }
  143. return -EINVAL;
  144. }
  145. static void opt3001_to_iio_ret(struct opt3001 *opt, u8 exponent,
  146. u16 mantissa, int *val, int *val2)
  147. {
  148. int lux;
  149. lux = 10 * (mantissa << exponent);
  150. *val = lux / 1000;
  151. *val2 = (lux - (*val * 1000)) * 1000;
  152. }
  153. static void opt3001_set_mode(struct opt3001 *opt, u16 *reg, u16 mode)
  154. {
  155. *reg &= ~OPT3001_CONFIGURATION_M_MASK;
  156. *reg |= mode;
  157. opt->mode = mode;
  158. }
  159. static IIO_CONST_ATTR_INT_TIME_AVAIL("0.1 0.8");
  160. static struct attribute *opt3001_attributes[] = {
  161. &iio_const_attr_integration_time_available.dev_attr.attr,
  162. NULL
  163. };
  164. static const struct attribute_group opt3001_attribute_group = {
  165. .attrs = opt3001_attributes,
  166. };
  167. static const struct iio_event_spec opt3001_event_spec[] = {
  168. {
  169. .type = IIO_EV_TYPE_THRESH,
  170. .dir = IIO_EV_DIR_RISING,
  171. .mask_separate = BIT(IIO_EV_INFO_VALUE) |
  172. BIT(IIO_EV_INFO_ENABLE),
  173. },
  174. {
  175. .type = IIO_EV_TYPE_THRESH,
  176. .dir = IIO_EV_DIR_FALLING,
  177. .mask_separate = BIT(IIO_EV_INFO_VALUE) |
  178. BIT(IIO_EV_INFO_ENABLE),
  179. },
  180. };
  181. static const struct iio_chan_spec opt3001_channels[] = {
  182. {
  183. .type = IIO_LIGHT,
  184. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
  185. BIT(IIO_CHAN_INFO_INT_TIME),
  186. .event_spec = opt3001_event_spec,
  187. .num_event_specs = ARRAY_SIZE(opt3001_event_spec),
  188. },
  189. IIO_CHAN_SOFT_TIMESTAMP(1),
  190. };
  191. static int opt3001_get_lux(struct opt3001 *opt, int *val, int *val2)
  192. {
  193. int ret;
  194. u16 mantissa;
  195. u16 reg;
  196. u8 exponent;
  197. u16 value;
  198. long timeout;
  199. if (opt->use_irq) {
  200. /*
  201. * Enable the end-of-conversion interrupt mechanism. Note that
  202. * doing so will overwrite the low-level limit value however we
  203. * will restore this value later on.
  204. */
  205. ret = i2c_smbus_write_word_swapped(opt->client,
  206. OPT3001_LOW_LIMIT,
  207. OPT3001_LOW_LIMIT_EOC_ENABLE);
  208. if (ret < 0) {
  209. dev_err(opt->dev, "failed to write register %02x\n",
  210. OPT3001_LOW_LIMIT);
  211. return ret;
  212. }
  213. /* Allow IRQ to access the device despite lock being set */
  214. opt->ok_to_ignore_lock = true;
  215. }
  216. /* Reset data-ready indicator flag */
  217. opt->result_ready = false;
  218. /* Configure for single-conversion mode and start a new conversion */
  219. ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION);
  220. if (ret < 0) {
  221. dev_err(opt->dev, "failed to read register %02x\n",
  222. OPT3001_CONFIGURATION);
  223. goto err;
  224. }
  225. reg = ret;
  226. opt3001_set_mode(opt, &reg, OPT3001_CONFIGURATION_M_SINGLE);
  227. ret = i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION,
  228. reg);
  229. if (ret < 0) {
  230. dev_err(opt->dev, "failed to write register %02x\n",
  231. OPT3001_CONFIGURATION);
  232. goto err;
  233. }
  234. if (opt->use_irq) {
  235. /* Wait for the IRQ to indicate the conversion is complete */
  236. ret = wait_event_timeout(opt->result_ready_queue,
  237. opt->result_ready,
  238. msecs_to_jiffies(OPT3001_RESULT_READY_LONG));
  239. if (ret == 0)
  240. return -ETIMEDOUT;
  241. } else {
  242. /* Sleep for result ready time */
  243. timeout = (opt->int_time == OPT3001_INT_TIME_SHORT) ?
  244. OPT3001_RESULT_READY_SHORT : OPT3001_RESULT_READY_LONG;
  245. msleep(timeout);
  246. /* Check result ready flag */
  247. ret = i2c_smbus_read_word_swapped(opt->client,
  248. OPT3001_CONFIGURATION);
  249. if (ret < 0) {
  250. dev_err(opt->dev, "failed to read register %02x\n",
  251. OPT3001_CONFIGURATION);
  252. goto err;
  253. }
  254. if (!(ret & OPT3001_CONFIGURATION_CRF)) {
  255. ret = -ETIMEDOUT;
  256. goto err;
  257. }
  258. /* Obtain value */
  259. ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_RESULT);
  260. if (ret < 0) {
  261. dev_err(opt->dev, "failed to read register %02x\n",
  262. OPT3001_RESULT);
  263. goto err;
  264. }
  265. opt->result = ret;
  266. opt->result_ready = true;
  267. }
  268. err:
  269. if (opt->use_irq)
  270. /* Disallow IRQ to access the device while lock is active */
  271. opt->ok_to_ignore_lock = false;
  272. if (ret < 0)
  273. return ret;
  274. if (opt->use_irq) {
  275. /*
  276. * Disable the end-of-conversion interrupt mechanism by
  277. * restoring the low-level limit value (clearing
  278. * OPT3001_LOW_LIMIT_EOC_ENABLE). Note that selectively clearing
  279. * those enable bits would affect the actual limit value due to
  280. * bit-overlap and therefore can't be done.
  281. */
  282. value = (opt->low_thresh_exp << 12) | opt->low_thresh_mantissa;
  283. ret = i2c_smbus_write_word_swapped(opt->client,
  284. OPT3001_LOW_LIMIT,
  285. value);
  286. if (ret < 0) {
  287. dev_err(opt->dev, "failed to write register %02x\n",
  288. OPT3001_LOW_LIMIT);
  289. return ret;
  290. }
  291. }
  292. exponent = OPT3001_REG_EXPONENT(opt->result);
  293. mantissa = OPT3001_REG_MANTISSA(opt->result);
  294. opt3001_to_iio_ret(opt, exponent, mantissa, val, val2);
  295. return IIO_VAL_INT_PLUS_MICRO;
  296. }
  297. static int opt3001_get_int_time(struct opt3001 *opt, int *val, int *val2)
  298. {
  299. *val = 0;
  300. *val2 = opt->int_time;
  301. return IIO_VAL_INT_PLUS_MICRO;
  302. }
  303. static int opt3001_set_int_time(struct opt3001 *opt, int time)
  304. {
  305. int ret;
  306. u16 reg;
  307. ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION);
  308. if (ret < 0) {
  309. dev_err(opt->dev, "failed to read register %02x\n",
  310. OPT3001_CONFIGURATION);
  311. return ret;
  312. }
  313. reg = ret;
  314. switch (time) {
  315. case OPT3001_INT_TIME_SHORT:
  316. reg &= ~OPT3001_CONFIGURATION_CT;
  317. opt->int_time = OPT3001_INT_TIME_SHORT;
  318. break;
  319. case OPT3001_INT_TIME_LONG:
  320. reg |= OPT3001_CONFIGURATION_CT;
  321. opt->int_time = OPT3001_INT_TIME_LONG;
  322. break;
  323. default:
  324. return -EINVAL;
  325. }
  326. return i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION,
  327. reg);
  328. }
  329. static int opt3001_read_raw(struct iio_dev *iio,
  330. struct iio_chan_spec const *chan, int *val, int *val2,
  331. long mask)
  332. {
  333. struct opt3001 *opt = iio_priv(iio);
  334. int ret;
  335. if (opt->mode == OPT3001_CONFIGURATION_M_CONTINUOUS)
  336. return -EBUSY;
  337. if (chan->type != IIO_LIGHT)
  338. return -EINVAL;
  339. mutex_lock(&opt->lock);
  340. switch (mask) {
  341. case IIO_CHAN_INFO_PROCESSED:
  342. ret = opt3001_get_lux(opt, val, val2);
  343. break;
  344. case IIO_CHAN_INFO_INT_TIME:
  345. ret = opt3001_get_int_time(opt, val, val2);
  346. break;
  347. default:
  348. ret = -EINVAL;
  349. }
  350. mutex_unlock(&opt->lock);
  351. return ret;
  352. }
  353. static int opt3001_write_raw(struct iio_dev *iio,
  354. struct iio_chan_spec const *chan, int val, int val2,
  355. long mask)
  356. {
  357. struct opt3001 *opt = iio_priv(iio);
  358. int ret;
  359. if (opt->mode == OPT3001_CONFIGURATION_M_CONTINUOUS)
  360. return -EBUSY;
  361. if (chan->type != IIO_LIGHT)
  362. return -EINVAL;
  363. if (mask != IIO_CHAN_INFO_INT_TIME)
  364. return -EINVAL;
  365. if (val != 0)
  366. return -EINVAL;
  367. mutex_lock(&opt->lock);
  368. ret = opt3001_set_int_time(opt, val2);
  369. mutex_unlock(&opt->lock);
  370. return ret;
  371. }
  372. static int opt3001_read_event_value(struct iio_dev *iio,
  373. const struct iio_chan_spec *chan, enum iio_event_type type,
  374. enum iio_event_direction dir, enum iio_event_info info,
  375. int *val, int *val2)
  376. {
  377. struct opt3001 *opt = iio_priv(iio);
  378. int ret = IIO_VAL_INT_PLUS_MICRO;
  379. mutex_lock(&opt->lock);
  380. switch (dir) {
  381. case IIO_EV_DIR_RISING:
  382. opt3001_to_iio_ret(opt, opt->high_thresh_exp,
  383. opt->high_thresh_mantissa, val, val2);
  384. break;
  385. case IIO_EV_DIR_FALLING:
  386. opt3001_to_iio_ret(opt, opt->low_thresh_exp,
  387. opt->low_thresh_mantissa, val, val2);
  388. break;
  389. default:
  390. ret = -EINVAL;
  391. }
  392. mutex_unlock(&opt->lock);
  393. return ret;
  394. }
  395. static int opt3001_write_event_value(struct iio_dev *iio,
  396. const struct iio_chan_spec *chan, enum iio_event_type type,
  397. enum iio_event_direction dir, enum iio_event_info info,
  398. int val, int val2)
  399. {
  400. struct opt3001 *opt = iio_priv(iio);
  401. int ret;
  402. u16 mantissa;
  403. u16 value;
  404. u16 reg;
  405. u8 exponent;
  406. if (val < 0)
  407. return -EINVAL;
  408. mutex_lock(&opt->lock);
  409. ret = opt3001_find_scale(opt, val, val2, &exponent);
  410. if (ret < 0) {
  411. dev_err(opt->dev, "can't find scale for %d.%06u\n", val, val2);
  412. goto err;
  413. }
  414. mantissa = (((val * 1000) + (val2 / 1000)) / 10) >> exponent;
  415. value = (exponent << 12) | mantissa;
  416. switch (dir) {
  417. case IIO_EV_DIR_RISING:
  418. reg = OPT3001_HIGH_LIMIT;
  419. opt->high_thresh_mantissa = mantissa;
  420. opt->high_thresh_exp = exponent;
  421. break;
  422. case IIO_EV_DIR_FALLING:
  423. reg = OPT3001_LOW_LIMIT;
  424. opt->low_thresh_mantissa = mantissa;
  425. opt->low_thresh_exp = exponent;
  426. break;
  427. default:
  428. ret = -EINVAL;
  429. goto err;
  430. }
  431. ret = i2c_smbus_write_word_swapped(opt->client, reg, value);
  432. if (ret < 0) {
  433. dev_err(opt->dev, "failed to write register %02x\n", reg);
  434. goto err;
  435. }
  436. err:
  437. mutex_unlock(&opt->lock);
  438. return ret;
  439. }
  440. static int opt3001_read_event_config(struct iio_dev *iio,
  441. const struct iio_chan_spec *chan, enum iio_event_type type,
  442. enum iio_event_direction dir)
  443. {
  444. struct opt3001 *opt = iio_priv(iio);
  445. return opt->mode == OPT3001_CONFIGURATION_M_CONTINUOUS;
  446. }
  447. static int opt3001_write_event_config(struct iio_dev *iio,
  448. const struct iio_chan_spec *chan, enum iio_event_type type,
  449. enum iio_event_direction dir, int state)
  450. {
  451. struct opt3001 *opt = iio_priv(iio);
  452. int ret;
  453. u16 mode;
  454. u16 reg;
  455. if (state && opt->mode == OPT3001_CONFIGURATION_M_CONTINUOUS)
  456. return 0;
  457. if (!state && opt->mode == OPT3001_CONFIGURATION_M_SHUTDOWN)
  458. return 0;
  459. mutex_lock(&opt->lock);
  460. mode = state ? OPT3001_CONFIGURATION_M_CONTINUOUS
  461. : OPT3001_CONFIGURATION_M_SHUTDOWN;
  462. ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION);
  463. if (ret < 0) {
  464. dev_err(opt->dev, "failed to read register %02x\n",
  465. OPT3001_CONFIGURATION);
  466. goto err;
  467. }
  468. reg = ret;
  469. opt3001_set_mode(opt, &reg, mode);
  470. ret = i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION,
  471. reg);
  472. if (ret < 0) {
  473. dev_err(opt->dev, "failed to write register %02x\n",
  474. OPT3001_CONFIGURATION);
  475. goto err;
  476. }
  477. err:
  478. mutex_unlock(&opt->lock);
  479. return ret;
  480. }
  481. static const struct iio_info opt3001_info = {
  482. .attrs = &opt3001_attribute_group,
  483. .read_raw = opt3001_read_raw,
  484. .write_raw = opt3001_write_raw,
  485. .read_event_value = opt3001_read_event_value,
  486. .write_event_value = opt3001_write_event_value,
  487. .read_event_config = opt3001_read_event_config,
  488. .write_event_config = opt3001_write_event_config,
  489. };
  490. static int opt3001_read_id(struct opt3001 *opt)
  491. {
  492. char manufacturer[2];
  493. u16 device_id;
  494. int ret;
  495. ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_MANUFACTURER_ID);
  496. if (ret < 0) {
  497. dev_err(opt->dev, "failed to read register %02x\n",
  498. OPT3001_MANUFACTURER_ID);
  499. return ret;
  500. }
  501. manufacturer[0] = ret >> 8;
  502. manufacturer[1] = ret & 0xff;
  503. ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_DEVICE_ID);
  504. if (ret < 0) {
  505. dev_err(opt->dev, "failed to read register %02x\n",
  506. OPT3001_DEVICE_ID);
  507. return ret;
  508. }
  509. device_id = ret;
  510. dev_info(opt->dev, "Found %c%c OPT%04x\n", manufacturer[0],
  511. manufacturer[1], device_id);
  512. return 0;
  513. }
  514. static int opt3001_configure(struct opt3001 *opt)
  515. {
  516. int ret;
  517. u16 reg;
  518. ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION);
  519. if (ret < 0) {
  520. dev_err(opt->dev, "failed to read register %02x\n",
  521. OPT3001_CONFIGURATION);
  522. return ret;
  523. }
  524. reg = ret;
  525. /* Enable automatic full-scale setting mode */
  526. reg &= ~OPT3001_CONFIGURATION_RN_MASK;
  527. reg |= OPT3001_CONFIGURATION_RN_AUTO;
  528. /* Reflect status of the device's integration time setting */
  529. if (reg & OPT3001_CONFIGURATION_CT)
  530. opt->int_time = OPT3001_INT_TIME_LONG;
  531. else
  532. opt->int_time = OPT3001_INT_TIME_SHORT;
  533. /* Ensure device is in shutdown initially */
  534. opt3001_set_mode(opt, &reg, OPT3001_CONFIGURATION_M_SHUTDOWN);
  535. /* Configure for latched window-style comparison operation */
  536. reg |= OPT3001_CONFIGURATION_L;
  537. reg &= ~OPT3001_CONFIGURATION_POL;
  538. reg &= ~OPT3001_CONFIGURATION_ME;
  539. reg &= ~OPT3001_CONFIGURATION_FC_MASK;
  540. ret = i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION,
  541. reg);
  542. if (ret < 0) {
  543. dev_err(opt->dev, "failed to write register %02x\n",
  544. OPT3001_CONFIGURATION);
  545. return ret;
  546. }
  547. ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_LOW_LIMIT);
  548. if (ret < 0) {
  549. dev_err(opt->dev, "failed to read register %02x\n",
  550. OPT3001_LOW_LIMIT);
  551. return ret;
  552. }
  553. opt->low_thresh_mantissa = OPT3001_REG_MANTISSA(ret);
  554. opt->low_thresh_exp = OPT3001_REG_EXPONENT(ret);
  555. ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_HIGH_LIMIT);
  556. if (ret < 0) {
  557. dev_err(opt->dev, "failed to read register %02x\n",
  558. OPT3001_HIGH_LIMIT);
  559. return ret;
  560. }
  561. opt->high_thresh_mantissa = OPT3001_REG_MANTISSA(ret);
  562. opt->high_thresh_exp = OPT3001_REG_EXPONENT(ret);
  563. return 0;
  564. }
  565. static irqreturn_t opt3001_irq(int irq, void *_iio)
  566. {
  567. struct iio_dev *iio = _iio;
  568. struct opt3001 *opt = iio_priv(iio);
  569. int ret;
  570. bool wake_result_ready_queue = false;
  571. if (!opt->ok_to_ignore_lock)
  572. mutex_lock(&opt->lock);
  573. ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION);
  574. if (ret < 0) {
  575. dev_err(opt->dev, "failed to read register %02x\n",
  576. OPT3001_CONFIGURATION);
  577. goto out;
  578. }
  579. if ((ret & OPT3001_CONFIGURATION_M_MASK) ==
  580. OPT3001_CONFIGURATION_M_CONTINUOUS) {
  581. if (ret & OPT3001_CONFIGURATION_FH)
  582. iio_push_event(iio,
  583. IIO_UNMOD_EVENT_CODE(IIO_LIGHT, 0,
  584. IIO_EV_TYPE_THRESH,
  585. IIO_EV_DIR_RISING),
  586. iio_get_time_ns(iio));
  587. if (ret & OPT3001_CONFIGURATION_FL)
  588. iio_push_event(iio,
  589. IIO_UNMOD_EVENT_CODE(IIO_LIGHT, 0,
  590. IIO_EV_TYPE_THRESH,
  591. IIO_EV_DIR_FALLING),
  592. iio_get_time_ns(iio));
  593. } else if (ret & OPT3001_CONFIGURATION_CRF) {
  594. ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_RESULT);
  595. if (ret < 0) {
  596. dev_err(opt->dev, "failed to read register %02x\n",
  597. OPT3001_RESULT);
  598. goto out;
  599. }
  600. opt->result = ret;
  601. opt->result_ready = true;
  602. wake_result_ready_queue = true;
  603. }
  604. out:
  605. if (!opt->ok_to_ignore_lock)
  606. mutex_unlock(&opt->lock);
  607. if (wake_result_ready_queue)
  608. wake_up(&opt->result_ready_queue);
  609. return IRQ_HANDLED;
  610. }
  611. static int opt3001_probe(struct i2c_client *client,
  612. const struct i2c_device_id *id)
  613. {
  614. struct device *dev = &client->dev;
  615. struct iio_dev *iio;
  616. struct opt3001 *opt;
  617. int irq = client->irq;
  618. int ret;
  619. iio = devm_iio_device_alloc(dev, sizeof(*opt));
  620. if (!iio)
  621. return -ENOMEM;
  622. opt = iio_priv(iio);
  623. opt->client = client;
  624. opt->dev = dev;
  625. mutex_init(&opt->lock);
  626. init_waitqueue_head(&opt->result_ready_queue);
  627. i2c_set_clientdata(client, iio);
  628. ret = opt3001_read_id(opt);
  629. if (ret)
  630. return ret;
  631. ret = opt3001_configure(opt);
  632. if (ret)
  633. return ret;
  634. iio->name = client->name;
  635. iio->channels = opt3001_channels;
  636. iio->num_channels = ARRAY_SIZE(opt3001_channels);
  637. iio->modes = INDIO_DIRECT_MODE;
  638. iio->info = &opt3001_info;
  639. ret = devm_iio_device_register(dev, iio);
  640. if (ret) {
  641. dev_err(dev, "failed to register IIO device\n");
  642. return ret;
  643. }
  644. /* Make use of INT pin only if valid IRQ no. is given */
  645. if (irq > 0) {
  646. ret = request_threaded_irq(irq, NULL, opt3001_irq,
  647. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  648. "opt3001", iio);
  649. if (ret) {
  650. dev_err(dev, "failed to request IRQ #%d\n", irq);
  651. return ret;
  652. }
  653. opt->use_irq = true;
  654. } else {
  655. dev_dbg(opt->dev, "enabling interrupt-less operation\n");
  656. }
  657. return 0;
  658. }
  659. static void opt3001_remove(struct i2c_client *client)
  660. {
  661. struct iio_dev *iio = i2c_get_clientdata(client);
  662. struct opt3001 *opt = iio_priv(iio);
  663. int ret;
  664. u16 reg;
  665. if (opt->use_irq)
  666. free_irq(client->irq, iio);
  667. ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION);
  668. if (ret < 0) {
  669. dev_err(opt->dev, "failed to read register %02x\n",
  670. OPT3001_CONFIGURATION);
  671. return;
  672. }
  673. reg = ret;
  674. opt3001_set_mode(opt, &reg, OPT3001_CONFIGURATION_M_SHUTDOWN);
  675. ret = i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION,
  676. reg);
  677. if (ret < 0) {
  678. dev_err(opt->dev, "failed to write register %02x\n",
  679. OPT3001_CONFIGURATION);
  680. }
  681. }
  682. static const struct i2c_device_id opt3001_id[] = {
  683. { "opt3001", 0 },
  684. { } /* Terminating Entry */
  685. };
  686. MODULE_DEVICE_TABLE(i2c, opt3001_id);
  687. static const struct of_device_id opt3001_of_match[] = {
  688. { .compatible = "ti,opt3001" },
  689. { }
  690. };
  691. MODULE_DEVICE_TABLE(of, opt3001_of_match);
  692. static struct i2c_driver opt3001_driver = {
  693. .probe = opt3001_probe,
  694. .remove = opt3001_remove,
  695. .id_table = opt3001_id,
  696. .driver = {
  697. .name = "opt3001",
  698. .of_match_table = opt3001_of_match,
  699. },
  700. };
  701. module_i2c_driver(opt3001_driver);
  702. MODULE_LICENSE("GPL v2");
  703. MODULE_AUTHOR("Andreas Dannenberg <[email protected]>");
  704. MODULE_DESCRIPTION("Texas Instruments OPT3001 Light Sensor Driver");