lm3533-als.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  1. /*
  2. * lm3533-als.c -- LM3533 Ambient Light Sensor driver
  3. *
  4. * Copyright (C) 2011-2012 Texas Instruments
  5. *
  6. * Author: Johan Hovold <jhovold@gmail.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. */
  13. #include <linux/atomic.h>
  14. #include <linux/fs.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/io.h>
  17. #include <linux/iio/events.h>
  18. #include <linux/iio/iio.h>
  19. #include <linux/module.h>
  20. #include <linux/mutex.h>
  21. #include <linux/mfd/core.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/slab.h>
  24. #include <linux/uaccess.h>
  25. #include <linux/mfd/lm3533.h>
  26. #define LM3533_ALS_RESISTOR_MIN 1
  27. #define LM3533_ALS_RESISTOR_MAX 127
  28. #define LM3533_ALS_CHANNEL_CURRENT_MAX 2
  29. #define LM3533_ALS_THRESH_MAX 3
  30. #define LM3533_ALS_ZONE_MAX 4
  31. #define LM3533_REG_ALS_RESISTOR_SELECT 0x30
  32. #define LM3533_REG_ALS_CONF 0x31
  33. #define LM3533_REG_ALS_ZONE_INFO 0x34
  34. #define LM3533_REG_ALS_READ_ADC_RAW 0x37
  35. #define LM3533_REG_ALS_READ_ADC_AVERAGE 0x38
  36. #define LM3533_REG_ALS_BOUNDARY_BASE 0x50
  37. #define LM3533_REG_ALS_TARGET_BASE 0x60
  38. #define LM3533_ALS_ENABLE_MASK 0x01
  39. #define LM3533_ALS_INPUT_MODE_MASK 0x02
  40. #define LM3533_ALS_INT_ENABLE_MASK 0x01
  41. #define LM3533_ALS_ZONE_SHIFT 2
  42. #define LM3533_ALS_ZONE_MASK 0x1c
  43. #define LM3533_ALS_FLAG_INT_ENABLED 1
  44. struct lm3533_als {
  45. struct lm3533 *lm3533;
  46. struct platform_device *pdev;
  47. unsigned long flags;
  48. int irq;
  49. atomic_t zone;
  50. struct mutex thresh_mutex;
  51. };
  52. static int lm3533_als_get_adc(struct iio_dev *indio_dev, bool average,
  53. int *adc)
  54. {
  55. struct lm3533_als *als = iio_priv(indio_dev);
  56. u8 reg;
  57. u8 val;
  58. int ret;
  59. if (average)
  60. reg = LM3533_REG_ALS_READ_ADC_AVERAGE;
  61. else
  62. reg = LM3533_REG_ALS_READ_ADC_RAW;
  63. ret = lm3533_read(als->lm3533, reg, &val);
  64. if (ret) {
  65. dev_err(&indio_dev->dev, "failed to read adc\n");
  66. return ret;
  67. }
  68. *adc = val;
  69. return 0;
  70. }
  71. static int _lm3533_als_get_zone(struct iio_dev *indio_dev, u8 *zone)
  72. {
  73. struct lm3533_als *als = iio_priv(indio_dev);
  74. u8 val;
  75. int ret;
  76. ret = lm3533_read(als->lm3533, LM3533_REG_ALS_ZONE_INFO, &val);
  77. if (ret) {
  78. dev_err(&indio_dev->dev, "failed to read zone\n");
  79. return ret;
  80. }
  81. val = (val & LM3533_ALS_ZONE_MASK) >> LM3533_ALS_ZONE_SHIFT;
  82. *zone = min_t(u8, val, LM3533_ALS_ZONE_MAX);
  83. return 0;
  84. }
  85. static int lm3533_als_get_zone(struct iio_dev *indio_dev, u8 *zone)
  86. {
  87. struct lm3533_als *als = iio_priv(indio_dev);
  88. int ret;
  89. if (test_bit(LM3533_ALS_FLAG_INT_ENABLED, &als->flags)) {
  90. *zone = atomic_read(&als->zone);
  91. } else {
  92. ret = _lm3533_als_get_zone(indio_dev, zone);
  93. if (ret)
  94. return ret;
  95. }
  96. return 0;
  97. }
  98. /*
  99. * channel output channel 0..2
  100. * zone zone 0..4
  101. */
  102. static inline u8 lm3533_als_get_target_reg(unsigned channel, unsigned zone)
  103. {
  104. return LM3533_REG_ALS_TARGET_BASE + 5 * channel + zone;
  105. }
  106. static int lm3533_als_get_target(struct iio_dev *indio_dev, unsigned channel,
  107. unsigned zone, u8 *val)
  108. {
  109. struct lm3533_als *als = iio_priv(indio_dev);
  110. u8 reg;
  111. int ret;
  112. if (channel > LM3533_ALS_CHANNEL_CURRENT_MAX)
  113. return -EINVAL;
  114. if (zone > LM3533_ALS_ZONE_MAX)
  115. return -EINVAL;
  116. reg = lm3533_als_get_target_reg(channel, zone);
  117. ret = lm3533_read(als->lm3533, reg, val);
  118. if (ret)
  119. dev_err(&indio_dev->dev, "failed to get target current\n");
  120. return ret;
  121. }
  122. static int lm3533_als_set_target(struct iio_dev *indio_dev, unsigned channel,
  123. unsigned zone, u8 val)
  124. {
  125. struct lm3533_als *als = iio_priv(indio_dev);
  126. u8 reg;
  127. int ret;
  128. if (channel > LM3533_ALS_CHANNEL_CURRENT_MAX)
  129. return -EINVAL;
  130. if (zone > LM3533_ALS_ZONE_MAX)
  131. return -EINVAL;
  132. reg = lm3533_als_get_target_reg(channel, zone);
  133. ret = lm3533_write(als->lm3533, reg, val);
  134. if (ret)
  135. dev_err(&indio_dev->dev, "failed to set target current\n");
  136. return ret;
  137. }
  138. static int lm3533_als_get_current(struct iio_dev *indio_dev, unsigned channel,
  139. int *val)
  140. {
  141. u8 zone;
  142. u8 target;
  143. int ret;
  144. ret = lm3533_als_get_zone(indio_dev, &zone);
  145. if (ret)
  146. return ret;
  147. ret = lm3533_als_get_target(indio_dev, channel, zone, &target);
  148. if (ret)
  149. return ret;
  150. *val = target;
  151. return 0;
  152. }
  153. static int lm3533_als_read_raw(struct iio_dev *indio_dev,
  154. struct iio_chan_spec const *chan,
  155. int *val, int *val2, long mask)
  156. {
  157. int ret;
  158. switch (mask) {
  159. case IIO_CHAN_INFO_RAW:
  160. switch (chan->type) {
  161. case IIO_LIGHT:
  162. ret = lm3533_als_get_adc(indio_dev, false, val);
  163. break;
  164. case IIO_CURRENT:
  165. ret = lm3533_als_get_current(indio_dev, chan->channel,
  166. val);
  167. break;
  168. default:
  169. return -EINVAL;
  170. }
  171. break;
  172. case IIO_CHAN_INFO_AVERAGE_RAW:
  173. ret = lm3533_als_get_adc(indio_dev, true, val);
  174. break;
  175. default:
  176. return -EINVAL;
  177. }
  178. if (ret)
  179. return ret;
  180. return IIO_VAL_INT;
  181. }
  182. #define CHANNEL_CURRENT(_channel) \
  183. { \
  184. .type = IIO_CURRENT, \
  185. .channel = _channel, \
  186. .indexed = true, \
  187. .output = true, \
  188. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  189. }
  190. static const struct iio_chan_spec lm3533_als_channels[] = {
  191. {
  192. .type = IIO_LIGHT,
  193. .channel = 0,
  194. .indexed = true,
  195. .info_mask_separate = BIT(IIO_CHAN_INFO_AVERAGE_RAW) |
  196. BIT(IIO_CHAN_INFO_RAW),
  197. },
  198. CHANNEL_CURRENT(0),
  199. CHANNEL_CURRENT(1),
  200. CHANNEL_CURRENT(2),
  201. };
  202. static irqreturn_t lm3533_als_isr(int irq, void *dev_id)
  203. {
  204. struct iio_dev *indio_dev = dev_id;
  205. struct lm3533_als *als = iio_priv(indio_dev);
  206. u8 zone;
  207. int ret;
  208. /* Clear interrupt by reading the ALS zone register. */
  209. ret = _lm3533_als_get_zone(indio_dev, &zone);
  210. if (ret)
  211. goto out;
  212. atomic_set(&als->zone, zone);
  213. iio_push_event(indio_dev,
  214. IIO_UNMOD_EVENT_CODE(IIO_LIGHT,
  215. 0,
  216. IIO_EV_TYPE_THRESH,
  217. IIO_EV_DIR_EITHER),
  218. iio_get_time_ns(indio_dev));
  219. out:
  220. return IRQ_HANDLED;
  221. }
  222. static int lm3533_als_set_int_mode(struct iio_dev *indio_dev, int enable)
  223. {
  224. struct lm3533_als *als = iio_priv(indio_dev);
  225. u8 mask = LM3533_ALS_INT_ENABLE_MASK;
  226. u8 val;
  227. int ret;
  228. if (enable)
  229. val = mask;
  230. else
  231. val = 0;
  232. ret = lm3533_update(als->lm3533, LM3533_REG_ALS_ZONE_INFO, val, mask);
  233. if (ret) {
  234. dev_err(&indio_dev->dev, "failed to set int mode %d\n",
  235. enable);
  236. return ret;
  237. }
  238. return 0;
  239. }
  240. static int lm3533_als_get_int_mode(struct iio_dev *indio_dev, int *enable)
  241. {
  242. struct lm3533_als *als = iio_priv(indio_dev);
  243. u8 mask = LM3533_ALS_INT_ENABLE_MASK;
  244. u8 val;
  245. int ret;
  246. ret = lm3533_read(als->lm3533, LM3533_REG_ALS_ZONE_INFO, &val);
  247. if (ret) {
  248. dev_err(&indio_dev->dev, "failed to get int mode\n");
  249. return ret;
  250. }
  251. *enable = !!(val & mask);
  252. return 0;
  253. }
  254. static inline u8 lm3533_als_get_threshold_reg(unsigned nr, bool raising)
  255. {
  256. u8 offset = !raising;
  257. return LM3533_REG_ALS_BOUNDARY_BASE + 2 * nr + offset;
  258. }
  259. static int lm3533_als_get_threshold(struct iio_dev *indio_dev, unsigned nr,
  260. bool raising, u8 *val)
  261. {
  262. struct lm3533_als *als = iio_priv(indio_dev);
  263. u8 reg;
  264. int ret;
  265. if (nr > LM3533_ALS_THRESH_MAX)
  266. return -EINVAL;
  267. reg = lm3533_als_get_threshold_reg(nr, raising);
  268. ret = lm3533_read(als->lm3533, reg, val);
  269. if (ret)
  270. dev_err(&indio_dev->dev, "failed to get threshold\n");
  271. return ret;
  272. }
  273. static int lm3533_als_set_threshold(struct iio_dev *indio_dev, unsigned nr,
  274. bool raising, u8 val)
  275. {
  276. struct lm3533_als *als = iio_priv(indio_dev);
  277. u8 val2;
  278. u8 reg, reg2;
  279. int ret;
  280. if (nr > LM3533_ALS_THRESH_MAX)
  281. return -EINVAL;
  282. reg = lm3533_als_get_threshold_reg(nr, raising);
  283. reg2 = lm3533_als_get_threshold_reg(nr, !raising);
  284. mutex_lock(&als->thresh_mutex);
  285. ret = lm3533_read(als->lm3533, reg2, &val2);
  286. if (ret) {
  287. dev_err(&indio_dev->dev, "failed to get threshold\n");
  288. goto out;
  289. }
  290. /*
  291. * This device does not allow negative hysteresis (in fact, it uses
  292. * whichever value is smaller as the lower bound) so we need to make
  293. * sure that thresh_falling <= thresh_raising.
  294. */
  295. if ((raising && (val < val2)) || (!raising && (val > val2))) {
  296. ret = -EINVAL;
  297. goto out;
  298. }
  299. ret = lm3533_write(als->lm3533, reg, val);
  300. if (ret) {
  301. dev_err(&indio_dev->dev, "failed to set threshold\n");
  302. goto out;
  303. }
  304. out:
  305. mutex_unlock(&als->thresh_mutex);
  306. return ret;
  307. }
  308. static int lm3533_als_get_hysteresis(struct iio_dev *indio_dev, unsigned nr,
  309. u8 *val)
  310. {
  311. struct lm3533_als *als = iio_priv(indio_dev);
  312. u8 falling;
  313. u8 raising;
  314. int ret;
  315. if (nr > LM3533_ALS_THRESH_MAX)
  316. return -EINVAL;
  317. mutex_lock(&als->thresh_mutex);
  318. ret = lm3533_als_get_threshold(indio_dev, nr, false, &falling);
  319. if (ret)
  320. goto out;
  321. ret = lm3533_als_get_threshold(indio_dev, nr, true, &raising);
  322. if (ret)
  323. goto out;
  324. *val = raising - falling;
  325. out:
  326. mutex_unlock(&als->thresh_mutex);
  327. return ret;
  328. }
  329. static ssize_t show_thresh_either_en(struct device *dev,
  330. struct device_attribute *attr,
  331. char *buf)
  332. {
  333. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  334. struct lm3533_als *als = iio_priv(indio_dev);
  335. int enable;
  336. int ret;
  337. if (als->irq) {
  338. ret = lm3533_als_get_int_mode(indio_dev, &enable);
  339. if (ret)
  340. return ret;
  341. } else {
  342. enable = 0;
  343. }
  344. return scnprintf(buf, PAGE_SIZE, "%u\n", enable);
  345. }
  346. static ssize_t store_thresh_either_en(struct device *dev,
  347. struct device_attribute *attr,
  348. const char *buf, size_t len)
  349. {
  350. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  351. struct lm3533_als *als = iio_priv(indio_dev);
  352. unsigned long enable;
  353. bool int_enabled;
  354. u8 zone;
  355. int ret;
  356. if (!als->irq)
  357. return -EBUSY;
  358. if (kstrtoul(buf, 0, &enable))
  359. return -EINVAL;
  360. int_enabled = test_bit(LM3533_ALS_FLAG_INT_ENABLED, &als->flags);
  361. if (enable && !int_enabled) {
  362. ret = lm3533_als_get_zone(indio_dev, &zone);
  363. if (ret)
  364. return ret;
  365. atomic_set(&als->zone, zone);
  366. set_bit(LM3533_ALS_FLAG_INT_ENABLED, &als->flags);
  367. }
  368. ret = lm3533_als_set_int_mode(indio_dev, enable);
  369. if (ret) {
  370. if (!int_enabled)
  371. clear_bit(LM3533_ALS_FLAG_INT_ENABLED, &als->flags);
  372. return ret;
  373. }
  374. if (!enable)
  375. clear_bit(LM3533_ALS_FLAG_INT_ENABLED, &als->flags);
  376. return len;
  377. }
  378. static ssize_t show_zone(struct device *dev,
  379. struct device_attribute *attr, char *buf)
  380. {
  381. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  382. u8 zone;
  383. int ret;
  384. ret = lm3533_als_get_zone(indio_dev, &zone);
  385. if (ret)
  386. return ret;
  387. return scnprintf(buf, PAGE_SIZE, "%u\n", zone);
  388. }
  389. enum lm3533_als_attribute_type {
  390. LM3533_ATTR_TYPE_HYSTERESIS,
  391. LM3533_ATTR_TYPE_TARGET,
  392. LM3533_ATTR_TYPE_THRESH_FALLING,
  393. LM3533_ATTR_TYPE_THRESH_RAISING,
  394. };
  395. struct lm3533_als_attribute {
  396. struct device_attribute dev_attr;
  397. enum lm3533_als_attribute_type type;
  398. u8 val1;
  399. u8 val2;
  400. };
  401. static inline struct lm3533_als_attribute *
  402. to_lm3533_als_attr(struct device_attribute *attr)
  403. {
  404. return container_of(attr, struct lm3533_als_attribute, dev_attr);
  405. }
  406. static ssize_t show_als_attr(struct device *dev,
  407. struct device_attribute *attr,
  408. char *buf)
  409. {
  410. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  411. struct lm3533_als_attribute *als_attr = to_lm3533_als_attr(attr);
  412. u8 val;
  413. int ret;
  414. switch (als_attr->type) {
  415. case LM3533_ATTR_TYPE_HYSTERESIS:
  416. ret = lm3533_als_get_hysteresis(indio_dev, als_attr->val1,
  417. &val);
  418. break;
  419. case LM3533_ATTR_TYPE_TARGET:
  420. ret = lm3533_als_get_target(indio_dev, als_attr->val1,
  421. als_attr->val2, &val);
  422. break;
  423. case LM3533_ATTR_TYPE_THRESH_FALLING:
  424. ret = lm3533_als_get_threshold(indio_dev, als_attr->val1,
  425. false, &val);
  426. break;
  427. case LM3533_ATTR_TYPE_THRESH_RAISING:
  428. ret = lm3533_als_get_threshold(indio_dev, als_attr->val1,
  429. true, &val);
  430. break;
  431. default:
  432. ret = -ENXIO;
  433. }
  434. if (ret)
  435. return ret;
  436. return scnprintf(buf, PAGE_SIZE, "%u\n", val);
  437. }
  438. static ssize_t store_als_attr(struct device *dev,
  439. struct device_attribute *attr,
  440. const char *buf, size_t len)
  441. {
  442. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  443. struct lm3533_als_attribute *als_attr = to_lm3533_als_attr(attr);
  444. u8 val;
  445. int ret;
  446. if (kstrtou8(buf, 0, &val))
  447. return -EINVAL;
  448. switch (als_attr->type) {
  449. case LM3533_ATTR_TYPE_TARGET:
  450. ret = lm3533_als_set_target(indio_dev, als_attr->val1,
  451. als_attr->val2, val);
  452. break;
  453. case LM3533_ATTR_TYPE_THRESH_FALLING:
  454. ret = lm3533_als_set_threshold(indio_dev, als_attr->val1,
  455. false, val);
  456. break;
  457. case LM3533_ATTR_TYPE_THRESH_RAISING:
  458. ret = lm3533_als_set_threshold(indio_dev, als_attr->val1,
  459. true, val);
  460. break;
  461. default:
  462. ret = -ENXIO;
  463. }
  464. if (ret)
  465. return ret;
  466. return len;
  467. }
  468. #define ALS_ATTR(_name, _mode, _show, _store, _type, _val1, _val2) \
  469. { .dev_attr = __ATTR(_name, _mode, _show, _store), \
  470. .type = _type, \
  471. .val1 = _val1, \
  472. .val2 = _val2 }
  473. #define LM3533_ALS_ATTR(_name, _mode, _show, _store, _type, _val1, _val2) \
  474. struct lm3533_als_attribute lm3533_als_attr_##_name = \
  475. ALS_ATTR(_name, _mode, _show, _store, _type, _val1, _val2)
  476. #define ALS_TARGET_ATTR_RW(_channel, _zone) \
  477. LM3533_ALS_ATTR(out_current##_channel##_current##_zone##_raw, \
  478. S_IRUGO | S_IWUSR, \
  479. show_als_attr, store_als_attr, \
  480. LM3533_ATTR_TYPE_TARGET, _channel, _zone)
  481. /*
  482. * ALS output current values (ALS mapper targets)
  483. *
  484. * out_current[0-2]_current[0-4]_raw 0-255
  485. */
  486. static ALS_TARGET_ATTR_RW(0, 0);
  487. static ALS_TARGET_ATTR_RW(0, 1);
  488. static ALS_TARGET_ATTR_RW(0, 2);
  489. static ALS_TARGET_ATTR_RW(0, 3);
  490. static ALS_TARGET_ATTR_RW(0, 4);
  491. static ALS_TARGET_ATTR_RW(1, 0);
  492. static ALS_TARGET_ATTR_RW(1, 1);
  493. static ALS_TARGET_ATTR_RW(1, 2);
  494. static ALS_TARGET_ATTR_RW(1, 3);
  495. static ALS_TARGET_ATTR_RW(1, 4);
  496. static ALS_TARGET_ATTR_RW(2, 0);
  497. static ALS_TARGET_ATTR_RW(2, 1);
  498. static ALS_TARGET_ATTR_RW(2, 2);
  499. static ALS_TARGET_ATTR_RW(2, 3);
  500. static ALS_TARGET_ATTR_RW(2, 4);
  501. #define ALS_THRESH_FALLING_ATTR_RW(_nr) \
  502. LM3533_ALS_ATTR(in_illuminance0_thresh##_nr##_falling_value, \
  503. S_IRUGO | S_IWUSR, \
  504. show_als_attr, store_als_attr, \
  505. LM3533_ATTR_TYPE_THRESH_FALLING, _nr, 0)
  506. #define ALS_THRESH_RAISING_ATTR_RW(_nr) \
  507. LM3533_ALS_ATTR(in_illuminance0_thresh##_nr##_raising_value, \
  508. S_IRUGO | S_IWUSR, \
  509. show_als_attr, store_als_attr, \
  510. LM3533_ATTR_TYPE_THRESH_RAISING, _nr, 0)
  511. /*
  512. * ALS Zone thresholds (boundaries)
  513. *
  514. * in_illuminance0_thresh[0-3]_falling_value 0-255
  515. * in_illuminance0_thresh[0-3]_raising_value 0-255
  516. */
  517. static ALS_THRESH_FALLING_ATTR_RW(0);
  518. static ALS_THRESH_FALLING_ATTR_RW(1);
  519. static ALS_THRESH_FALLING_ATTR_RW(2);
  520. static ALS_THRESH_FALLING_ATTR_RW(3);
  521. static ALS_THRESH_RAISING_ATTR_RW(0);
  522. static ALS_THRESH_RAISING_ATTR_RW(1);
  523. static ALS_THRESH_RAISING_ATTR_RW(2);
  524. static ALS_THRESH_RAISING_ATTR_RW(3);
  525. #define ALS_HYSTERESIS_ATTR_RO(_nr) \
  526. LM3533_ALS_ATTR(in_illuminance0_thresh##_nr##_hysteresis, \
  527. S_IRUGO, show_als_attr, NULL, \
  528. LM3533_ATTR_TYPE_HYSTERESIS, _nr, 0)
  529. /*
  530. * ALS Zone threshold hysteresis
  531. *
  532. * threshY_hysteresis = threshY_raising - threshY_falling
  533. *
  534. * in_illuminance0_thresh[0-3]_hysteresis 0-255
  535. * in_illuminance0_thresh[0-3]_hysteresis 0-255
  536. */
  537. static ALS_HYSTERESIS_ATTR_RO(0);
  538. static ALS_HYSTERESIS_ATTR_RO(1);
  539. static ALS_HYSTERESIS_ATTR_RO(2);
  540. static ALS_HYSTERESIS_ATTR_RO(3);
  541. #define ILLUMINANCE_ATTR_RO(_name) \
  542. DEVICE_ATTR(in_illuminance0_##_name, S_IRUGO, show_##_name, NULL)
  543. #define ILLUMINANCE_ATTR_RW(_name) \
  544. DEVICE_ATTR(in_illuminance0_##_name, S_IRUGO | S_IWUSR, \
  545. show_##_name, store_##_name)
  546. /*
  547. * ALS Zone threshold-event enable
  548. *
  549. * in_illuminance0_thresh_either_en 0,1
  550. */
  551. static ILLUMINANCE_ATTR_RW(thresh_either_en);
  552. /*
  553. * ALS Current Zone
  554. *
  555. * in_illuminance0_zone 0-4
  556. */
  557. static ILLUMINANCE_ATTR_RO(zone);
  558. static struct attribute *lm3533_als_event_attributes[] = {
  559. &dev_attr_in_illuminance0_thresh_either_en.attr,
  560. &lm3533_als_attr_in_illuminance0_thresh0_falling_value.dev_attr.attr,
  561. &lm3533_als_attr_in_illuminance0_thresh0_hysteresis.dev_attr.attr,
  562. &lm3533_als_attr_in_illuminance0_thresh0_raising_value.dev_attr.attr,
  563. &lm3533_als_attr_in_illuminance0_thresh1_falling_value.dev_attr.attr,
  564. &lm3533_als_attr_in_illuminance0_thresh1_hysteresis.dev_attr.attr,
  565. &lm3533_als_attr_in_illuminance0_thresh1_raising_value.dev_attr.attr,
  566. &lm3533_als_attr_in_illuminance0_thresh2_falling_value.dev_attr.attr,
  567. &lm3533_als_attr_in_illuminance0_thresh2_hysteresis.dev_attr.attr,
  568. &lm3533_als_attr_in_illuminance0_thresh2_raising_value.dev_attr.attr,
  569. &lm3533_als_attr_in_illuminance0_thresh3_falling_value.dev_attr.attr,
  570. &lm3533_als_attr_in_illuminance0_thresh3_hysteresis.dev_attr.attr,
  571. &lm3533_als_attr_in_illuminance0_thresh3_raising_value.dev_attr.attr,
  572. NULL
  573. };
  574. static const struct attribute_group lm3533_als_event_attribute_group = {
  575. .attrs = lm3533_als_event_attributes
  576. };
  577. static struct attribute *lm3533_als_attributes[] = {
  578. &dev_attr_in_illuminance0_zone.attr,
  579. &lm3533_als_attr_out_current0_current0_raw.dev_attr.attr,
  580. &lm3533_als_attr_out_current0_current1_raw.dev_attr.attr,
  581. &lm3533_als_attr_out_current0_current2_raw.dev_attr.attr,
  582. &lm3533_als_attr_out_current0_current3_raw.dev_attr.attr,
  583. &lm3533_als_attr_out_current0_current4_raw.dev_attr.attr,
  584. &lm3533_als_attr_out_current1_current0_raw.dev_attr.attr,
  585. &lm3533_als_attr_out_current1_current1_raw.dev_attr.attr,
  586. &lm3533_als_attr_out_current1_current2_raw.dev_attr.attr,
  587. &lm3533_als_attr_out_current1_current3_raw.dev_attr.attr,
  588. &lm3533_als_attr_out_current1_current4_raw.dev_attr.attr,
  589. &lm3533_als_attr_out_current2_current0_raw.dev_attr.attr,
  590. &lm3533_als_attr_out_current2_current1_raw.dev_attr.attr,
  591. &lm3533_als_attr_out_current2_current2_raw.dev_attr.attr,
  592. &lm3533_als_attr_out_current2_current3_raw.dev_attr.attr,
  593. &lm3533_als_attr_out_current2_current4_raw.dev_attr.attr,
  594. NULL
  595. };
  596. static const struct attribute_group lm3533_als_attribute_group = {
  597. .attrs = lm3533_als_attributes
  598. };
  599. static int lm3533_als_set_input_mode(struct lm3533_als *als, bool pwm_mode)
  600. {
  601. u8 mask = LM3533_ALS_INPUT_MODE_MASK;
  602. u8 val;
  603. int ret;
  604. if (pwm_mode)
  605. val = mask; /* pwm input */
  606. else
  607. val = 0; /* analog input */
  608. ret = lm3533_update(als->lm3533, LM3533_REG_ALS_CONF, val, mask);
  609. if (ret) {
  610. dev_err(&als->pdev->dev, "failed to set input mode %d\n",
  611. pwm_mode);
  612. return ret;
  613. }
  614. return 0;
  615. }
  616. static int lm3533_als_set_resistor(struct lm3533_als *als, u8 val)
  617. {
  618. int ret;
  619. if (val < LM3533_ALS_RESISTOR_MIN || val > LM3533_ALS_RESISTOR_MAX) {
  620. dev_err(&als->pdev->dev, "invalid resistor value\n");
  621. return -EINVAL;
  622. };
  623. ret = lm3533_write(als->lm3533, LM3533_REG_ALS_RESISTOR_SELECT, val);
  624. if (ret) {
  625. dev_err(&als->pdev->dev, "failed to set resistor\n");
  626. return ret;
  627. }
  628. return 0;
  629. }
  630. static int lm3533_als_setup(struct lm3533_als *als,
  631. struct lm3533_als_platform_data *pdata)
  632. {
  633. int ret;
  634. ret = lm3533_als_set_input_mode(als, pdata->pwm_mode);
  635. if (ret)
  636. return ret;
  637. /* ALS input is always high impedance in PWM-mode. */
  638. if (!pdata->pwm_mode) {
  639. ret = lm3533_als_set_resistor(als, pdata->r_select);
  640. if (ret)
  641. return ret;
  642. }
  643. return 0;
  644. }
  645. static int lm3533_als_setup_irq(struct lm3533_als *als, void *dev)
  646. {
  647. u8 mask = LM3533_ALS_INT_ENABLE_MASK;
  648. int ret;
  649. /* Make sure interrupts are disabled. */
  650. ret = lm3533_update(als->lm3533, LM3533_REG_ALS_ZONE_INFO, 0, mask);
  651. if (ret) {
  652. dev_err(&als->pdev->dev, "failed to disable interrupts\n");
  653. return ret;
  654. }
  655. ret = request_threaded_irq(als->irq, NULL, lm3533_als_isr,
  656. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  657. dev_name(&als->pdev->dev), dev);
  658. if (ret) {
  659. dev_err(&als->pdev->dev, "failed to request irq %d\n",
  660. als->irq);
  661. return ret;
  662. }
  663. return 0;
  664. }
  665. static int lm3533_als_enable(struct lm3533_als *als)
  666. {
  667. u8 mask = LM3533_ALS_ENABLE_MASK;
  668. int ret;
  669. ret = lm3533_update(als->lm3533, LM3533_REG_ALS_CONF, mask, mask);
  670. if (ret)
  671. dev_err(&als->pdev->dev, "failed to enable ALS\n");
  672. return ret;
  673. }
  674. static int lm3533_als_disable(struct lm3533_als *als)
  675. {
  676. u8 mask = LM3533_ALS_ENABLE_MASK;
  677. int ret;
  678. ret = lm3533_update(als->lm3533, LM3533_REG_ALS_CONF, 0, mask);
  679. if (ret)
  680. dev_err(&als->pdev->dev, "failed to disable ALS\n");
  681. return ret;
  682. }
  683. static const struct iio_info lm3533_als_info = {
  684. .attrs = &lm3533_als_attribute_group,
  685. .event_attrs = &lm3533_als_event_attribute_group,
  686. .read_raw = &lm3533_als_read_raw,
  687. };
  688. static int lm3533_als_probe(struct platform_device *pdev)
  689. {
  690. struct lm3533 *lm3533;
  691. struct lm3533_als_platform_data *pdata;
  692. struct lm3533_als *als;
  693. struct iio_dev *indio_dev;
  694. int ret;
  695. lm3533 = dev_get_drvdata(pdev->dev.parent);
  696. if (!lm3533)
  697. return -EINVAL;
  698. pdata = pdev->dev.platform_data;
  699. if (!pdata) {
  700. dev_err(&pdev->dev, "no platform data\n");
  701. return -EINVAL;
  702. }
  703. indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*als));
  704. if (!indio_dev)
  705. return -ENOMEM;
  706. indio_dev->info = &lm3533_als_info;
  707. indio_dev->channels = lm3533_als_channels;
  708. indio_dev->num_channels = ARRAY_SIZE(lm3533_als_channels);
  709. indio_dev->name = dev_name(&pdev->dev);
  710. indio_dev->dev.parent = pdev->dev.parent;
  711. indio_dev->modes = INDIO_DIRECT_MODE;
  712. als = iio_priv(indio_dev);
  713. als->lm3533 = lm3533;
  714. als->pdev = pdev;
  715. als->irq = lm3533->irq;
  716. atomic_set(&als->zone, 0);
  717. mutex_init(&als->thresh_mutex);
  718. platform_set_drvdata(pdev, indio_dev);
  719. if (als->irq) {
  720. ret = lm3533_als_setup_irq(als, indio_dev);
  721. if (ret)
  722. return ret;
  723. }
  724. ret = lm3533_als_setup(als, pdata);
  725. if (ret)
  726. goto err_free_irq;
  727. ret = lm3533_als_enable(als);
  728. if (ret)
  729. goto err_free_irq;
  730. ret = iio_device_register(indio_dev);
  731. if (ret) {
  732. dev_err(&pdev->dev, "failed to register ALS\n");
  733. goto err_disable;
  734. }
  735. return 0;
  736. err_disable:
  737. lm3533_als_disable(als);
  738. err_free_irq:
  739. if (als->irq)
  740. free_irq(als->irq, indio_dev);
  741. return ret;
  742. }
  743. static int lm3533_als_remove(struct platform_device *pdev)
  744. {
  745. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  746. struct lm3533_als *als = iio_priv(indio_dev);
  747. lm3533_als_set_int_mode(indio_dev, false);
  748. iio_device_unregister(indio_dev);
  749. lm3533_als_disable(als);
  750. if (als->irq)
  751. free_irq(als->irq, indio_dev);
  752. return 0;
  753. }
  754. static struct platform_driver lm3533_als_driver = {
  755. .driver = {
  756. .name = "lm3533-als",
  757. },
  758. .probe = lm3533_als_probe,
  759. .remove = lm3533_als_remove,
  760. };
  761. module_platform_driver(lm3533_als_driver);
  762. MODULE_AUTHOR("Johan Hovold <jhovold@gmail.com>");
  763. MODULE_DESCRIPTION("LM3533 Ambient Light Sensor driver");
  764. MODULE_LICENSE("GPL");
  765. MODULE_ALIAS("platform:lm3533-als");