lm3533-als.c 22 KB

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