max44009.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * max44009.c - Support for MAX44009 Ambient Light Sensor
  4. *
  5. * Copyright (c) 2019 Robert Eshleman <[email protected]>
  6. *
  7. * Datasheet: https://datasheets.maximintegrated.com/en/ds/MAX44009.pdf
  8. *
  9. * TODO: Support continuous mode and configuring from manual mode to
  10. * automatic mode.
  11. *
  12. * Default I2C address: 0x4a
  13. */
  14. #include <linux/init.h>
  15. #include <linux/kernel.h>
  16. #include <linux/bits.h>
  17. #include <linux/i2c.h>
  18. #include <linux/iio/events.h>
  19. #include <linux/iio/iio.h>
  20. #include <linux/iio/sysfs.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/module.h>
  23. #include <linux/util_macros.h>
  24. #define MAX44009_DRV_NAME "max44009"
  25. /* Registers in datasheet order */
  26. #define MAX44009_REG_INT_STATUS 0x0
  27. #define MAX44009_REG_INT_EN 0x1
  28. #define MAX44009_REG_CFG 0x2
  29. #define MAX44009_REG_LUX_HI 0x3
  30. #define MAX44009_REG_LUX_LO 0x4
  31. #define MAX44009_REG_UPPER_THR 0x5
  32. #define MAX44009_REG_LOWER_THR 0x6
  33. #define MAX44009_REG_THR_TIMER 0x7
  34. #define MAX44009_CFG_TIM_MASK GENMASK(2, 0)
  35. #define MAX44009_CFG_MAN_MODE_MASK BIT(6)
  36. /* The maximum rising threshold for the max44009 */
  37. #define MAX44009_MAXIMUM_THRESHOLD 7520256
  38. #define MAX44009_THRESH_EXP_MASK (0xf << 4)
  39. #define MAX44009_THRESH_EXP_RSHIFT 4
  40. #define MAX44009_THRESH_MANT_LSHIFT 4
  41. #define MAX44009_THRESH_MANT_MASK 0xf
  42. #define MAX44009_UPPER_THR_MINIMUM 15
  43. /* The max44009 always scales raw readings by 0.045 and is non-configurable */
  44. #define MAX44009_SCALE_NUMERATOR 45
  45. #define MAX44009_SCALE_DENOMINATOR 1000
  46. /* The fixed-point fractional multiplier for de-scaling threshold values */
  47. #define MAX44009_FRACT_MULT 1000000
  48. static const u32 max44009_int_time_ns_array[] = {
  49. 800000000,
  50. 400000000,
  51. 200000000,
  52. 100000000,
  53. 50000000, /* Manual mode only */
  54. 25000000, /* Manual mode only */
  55. 12500000, /* Manual mode only */
  56. 6250000, /* Manual mode only */
  57. };
  58. static const char max44009_int_time_str[] =
  59. "0.8 "
  60. "0.4 "
  61. "0.2 "
  62. "0.1 "
  63. "0.05 "
  64. "0.025 "
  65. "0.0125 "
  66. "0.00625";
  67. struct max44009_data {
  68. struct i2c_client *client;
  69. struct mutex lock;
  70. };
  71. static const struct iio_event_spec max44009_event_spec[] = {
  72. {
  73. .type = IIO_EV_TYPE_THRESH,
  74. .dir = IIO_EV_DIR_RISING,
  75. .mask_separate = BIT(IIO_EV_INFO_VALUE) |
  76. BIT(IIO_EV_INFO_ENABLE),
  77. },
  78. {
  79. .type = IIO_EV_TYPE_THRESH,
  80. .dir = IIO_EV_DIR_FALLING,
  81. .mask_separate = BIT(IIO_EV_INFO_VALUE) |
  82. BIT(IIO_EV_INFO_ENABLE),
  83. },
  84. };
  85. static const struct iio_chan_spec max44009_channels[] = {
  86. {
  87. .type = IIO_LIGHT,
  88. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
  89. BIT(IIO_CHAN_INFO_INT_TIME),
  90. .event_spec = max44009_event_spec,
  91. .num_event_specs = ARRAY_SIZE(max44009_event_spec),
  92. },
  93. };
  94. static int max44009_read_int_time(struct max44009_data *data)
  95. {
  96. int ret = i2c_smbus_read_byte_data(data->client, MAX44009_REG_CFG);
  97. if (ret < 0)
  98. return ret;
  99. return max44009_int_time_ns_array[ret & MAX44009_CFG_TIM_MASK];
  100. }
  101. static int max44009_write_int_time(struct max44009_data *data,
  102. int val, int val2)
  103. {
  104. struct i2c_client *client = data->client;
  105. int ret, int_time, config;
  106. s64 ns;
  107. ns = val * NSEC_PER_SEC + val2;
  108. int_time = find_closest_descending(
  109. ns,
  110. max44009_int_time_ns_array,
  111. ARRAY_SIZE(max44009_int_time_ns_array));
  112. ret = i2c_smbus_read_byte_data(client, MAX44009_REG_CFG);
  113. if (ret < 0)
  114. return ret;
  115. config = ret;
  116. config &= int_time;
  117. /*
  118. * To set the integration time, the device must also be in manual
  119. * mode.
  120. */
  121. config |= MAX44009_CFG_MAN_MODE_MASK;
  122. return i2c_smbus_write_byte_data(client, MAX44009_REG_CFG, config);
  123. }
  124. static int max44009_write_raw(struct iio_dev *indio_dev,
  125. struct iio_chan_spec const *chan, int val,
  126. int val2, long mask)
  127. {
  128. struct max44009_data *data = iio_priv(indio_dev);
  129. int ret;
  130. if (mask == IIO_CHAN_INFO_INT_TIME && chan->type == IIO_LIGHT) {
  131. mutex_lock(&data->lock);
  132. ret = max44009_write_int_time(data, val, val2);
  133. mutex_unlock(&data->lock);
  134. return ret;
  135. }
  136. return -EINVAL;
  137. }
  138. static int max44009_write_raw_get_fmt(struct iio_dev *indio_dev,
  139. struct iio_chan_spec const *chan,
  140. long mask)
  141. {
  142. return IIO_VAL_INT_PLUS_NANO;
  143. }
  144. static int max44009_lux_raw(u8 hi, u8 lo)
  145. {
  146. int mantissa;
  147. int exponent;
  148. /*
  149. * The mantissa consists of the low nibble of the Lux High Byte
  150. * and the low nibble of the Lux Low Byte.
  151. */
  152. mantissa = ((hi & 0xf) << 4) | (lo & 0xf);
  153. /* The exponent byte is just the upper nibble of the Lux High Byte */
  154. exponent = (hi >> 4) & 0xf;
  155. /*
  156. * The exponent value is base 2 to the power of the raw exponent byte.
  157. */
  158. exponent = 1 << exponent;
  159. return exponent * mantissa;
  160. }
  161. #define MAX44009_READ_LUX_XFER_LEN (4)
  162. static int max44009_read_lux_raw(struct max44009_data *data)
  163. {
  164. int ret;
  165. u8 hireg = MAX44009_REG_LUX_HI;
  166. u8 loreg = MAX44009_REG_LUX_LO;
  167. u8 lo = 0;
  168. u8 hi = 0;
  169. struct i2c_msg msgs[] = {
  170. {
  171. .addr = data->client->addr,
  172. .flags = 0,
  173. .len = sizeof(hireg),
  174. .buf = &hireg,
  175. },
  176. {
  177. .addr = data->client->addr,
  178. .flags = I2C_M_RD,
  179. .len = sizeof(hi),
  180. .buf = &hi,
  181. },
  182. {
  183. .addr = data->client->addr,
  184. .flags = 0,
  185. .len = sizeof(loreg),
  186. .buf = &loreg,
  187. },
  188. {
  189. .addr = data->client->addr,
  190. .flags = I2C_M_RD,
  191. .len = sizeof(lo),
  192. .buf = &lo,
  193. }
  194. };
  195. /*
  196. * Use i2c_transfer instead of smbus read because i2c_transfer
  197. * does NOT use a stop bit between address write and data read.
  198. * Using a stop bit causes disjoint upper/lower byte reads and
  199. * reduces accuracy.
  200. */
  201. ret = i2c_transfer(data->client->adapter,
  202. msgs, MAX44009_READ_LUX_XFER_LEN);
  203. if (ret != MAX44009_READ_LUX_XFER_LEN)
  204. return -EIO;
  205. return max44009_lux_raw(hi, lo);
  206. }
  207. static int max44009_read_raw(struct iio_dev *indio_dev,
  208. struct iio_chan_spec const *chan, int *val,
  209. int *val2, long mask)
  210. {
  211. struct max44009_data *data = iio_priv(indio_dev);
  212. int lux_raw;
  213. int ret;
  214. switch (mask) {
  215. case IIO_CHAN_INFO_PROCESSED:
  216. switch (chan->type) {
  217. case IIO_LIGHT:
  218. ret = max44009_read_lux_raw(data);
  219. if (ret < 0)
  220. return ret;
  221. lux_raw = ret;
  222. *val = lux_raw * MAX44009_SCALE_NUMERATOR;
  223. *val2 = MAX44009_SCALE_DENOMINATOR;
  224. return IIO_VAL_FRACTIONAL;
  225. default:
  226. return -EINVAL;
  227. }
  228. case IIO_CHAN_INFO_INT_TIME:
  229. switch (chan->type) {
  230. case IIO_LIGHT:
  231. ret = max44009_read_int_time(data);
  232. if (ret < 0)
  233. return ret;
  234. *val2 = ret;
  235. *val = 0;
  236. return IIO_VAL_INT_PLUS_NANO;
  237. default:
  238. return -EINVAL;
  239. }
  240. default:
  241. return -EINVAL;
  242. }
  243. }
  244. static IIO_CONST_ATTR(illuminance_integration_time_available,
  245. max44009_int_time_str);
  246. static struct attribute *max44009_attributes[] = {
  247. &iio_const_attr_illuminance_integration_time_available.dev_attr.attr,
  248. NULL,
  249. };
  250. static const struct attribute_group max44009_attribute_group = {
  251. .attrs = max44009_attributes,
  252. };
  253. static int max44009_threshold_byte_from_fraction(int integral, int fractional)
  254. {
  255. int mantissa, exp;
  256. if ((integral <= 0 && fractional <= 0) ||
  257. integral > MAX44009_MAXIMUM_THRESHOLD ||
  258. (integral == MAX44009_MAXIMUM_THRESHOLD && fractional != 0))
  259. return -EINVAL;
  260. /* Reverse scaling of fixed-point integral */
  261. mantissa = integral * MAX44009_SCALE_DENOMINATOR;
  262. mantissa /= MAX44009_SCALE_NUMERATOR;
  263. /* Reverse scaling of fixed-point fractional */
  264. mantissa += fractional / MAX44009_FRACT_MULT *
  265. (MAX44009_SCALE_DENOMINATOR / MAX44009_SCALE_NUMERATOR);
  266. for (exp = 0; mantissa > 0xff; exp++)
  267. mantissa >>= 1;
  268. mantissa >>= 4;
  269. mantissa &= 0xf;
  270. exp <<= 4;
  271. return exp | mantissa;
  272. }
  273. static int max44009_get_thr_reg(enum iio_event_direction dir)
  274. {
  275. switch (dir) {
  276. case IIO_EV_DIR_RISING:
  277. return MAX44009_REG_UPPER_THR;
  278. case IIO_EV_DIR_FALLING:
  279. return MAX44009_REG_LOWER_THR;
  280. default:
  281. return -EINVAL;
  282. }
  283. }
  284. static int max44009_write_event_value(struct iio_dev *indio_dev,
  285. const struct iio_chan_spec *chan,
  286. enum iio_event_type type,
  287. enum iio_event_direction dir,
  288. enum iio_event_info info,
  289. int val, int val2)
  290. {
  291. struct max44009_data *data = iio_priv(indio_dev);
  292. int reg, threshold;
  293. if (info != IIO_EV_INFO_VALUE || chan->type != IIO_LIGHT)
  294. return -EINVAL;
  295. threshold = max44009_threshold_byte_from_fraction(val, val2);
  296. if (threshold < 0)
  297. return threshold;
  298. reg = max44009_get_thr_reg(dir);
  299. if (reg < 0)
  300. return reg;
  301. return i2c_smbus_write_byte_data(data->client, reg, threshold);
  302. }
  303. static int max44009_read_threshold(struct iio_dev *indio_dev,
  304. enum iio_event_direction dir)
  305. {
  306. struct max44009_data *data = iio_priv(indio_dev);
  307. int byte, reg;
  308. int mantissa, exponent;
  309. reg = max44009_get_thr_reg(dir);
  310. if (reg < 0)
  311. return reg;
  312. byte = i2c_smbus_read_byte_data(data->client, reg);
  313. if (byte < 0)
  314. return byte;
  315. mantissa = byte & MAX44009_THRESH_MANT_MASK;
  316. mantissa <<= MAX44009_THRESH_MANT_LSHIFT;
  317. /*
  318. * To get the upper threshold, always adds the minimum upper threshold
  319. * value to the shifted byte value (see datasheet).
  320. */
  321. if (dir == IIO_EV_DIR_RISING)
  322. mantissa += MAX44009_UPPER_THR_MINIMUM;
  323. /*
  324. * Exponent is base 2 to the power of the threshold exponent byte
  325. * value
  326. */
  327. exponent = byte & MAX44009_THRESH_EXP_MASK;
  328. exponent >>= MAX44009_THRESH_EXP_RSHIFT;
  329. return (1 << exponent) * mantissa;
  330. }
  331. static int max44009_read_event_value(struct iio_dev *indio_dev,
  332. const struct iio_chan_spec *chan,
  333. enum iio_event_type type,
  334. enum iio_event_direction dir,
  335. enum iio_event_info info,
  336. int *val, int *val2)
  337. {
  338. int ret;
  339. int threshold;
  340. if (chan->type != IIO_LIGHT || type != IIO_EV_TYPE_THRESH)
  341. return -EINVAL;
  342. ret = max44009_read_threshold(indio_dev, dir);
  343. if (ret < 0)
  344. return ret;
  345. threshold = ret;
  346. *val = threshold * MAX44009_SCALE_NUMERATOR;
  347. *val2 = MAX44009_SCALE_DENOMINATOR;
  348. return IIO_VAL_FRACTIONAL;
  349. }
  350. static int max44009_write_event_config(struct iio_dev *indio_dev,
  351. const struct iio_chan_spec *chan,
  352. enum iio_event_type type,
  353. enum iio_event_direction dir,
  354. int state)
  355. {
  356. struct max44009_data *data = iio_priv(indio_dev);
  357. int ret;
  358. if (chan->type != IIO_LIGHT || type != IIO_EV_TYPE_THRESH)
  359. return -EINVAL;
  360. ret = i2c_smbus_write_byte_data(data->client,
  361. MAX44009_REG_INT_EN, state);
  362. if (ret < 0)
  363. return ret;
  364. /*
  365. * Set device to trigger interrupt immediately upon exceeding
  366. * the threshold limit.
  367. */
  368. return i2c_smbus_write_byte_data(data->client,
  369. MAX44009_REG_THR_TIMER, 0);
  370. }
  371. static int max44009_read_event_config(struct iio_dev *indio_dev,
  372. const struct iio_chan_spec *chan,
  373. enum iio_event_type type,
  374. enum iio_event_direction dir)
  375. {
  376. struct max44009_data *data = iio_priv(indio_dev);
  377. if (chan->type != IIO_LIGHT || type != IIO_EV_TYPE_THRESH)
  378. return -EINVAL;
  379. return i2c_smbus_read_byte_data(data->client, MAX44009_REG_INT_EN);
  380. }
  381. static const struct iio_info max44009_info = {
  382. .read_raw = max44009_read_raw,
  383. .write_raw = max44009_write_raw,
  384. .write_raw_get_fmt = max44009_write_raw_get_fmt,
  385. .read_event_value = max44009_read_event_value,
  386. .read_event_config = max44009_read_event_config,
  387. .write_event_value = max44009_write_event_value,
  388. .write_event_config = max44009_write_event_config,
  389. .attrs = &max44009_attribute_group,
  390. };
  391. static irqreturn_t max44009_threaded_irq_handler(int irq, void *p)
  392. {
  393. struct iio_dev *indio_dev = p;
  394. struct max44009_data *data = iio_priv(indio_dev);
  395. int ret;
  396. ret = i2c_smbus_read_byte_data(data->client, MAX44009_REG_INT_STATUS);
  397. if (ret) {
  398. iio_push_event(indio_dev,
  399. IIO_UNMOD_EVENT_CODE(IIO_LIGHT, 0,
  400. IIO_EV_TYPE_THRESH,
  401. IIO_EV_DIR_EITHER),
  402. iio_get_time_ns(indio_dev));
  403. return IRQ_HANDLED;
  404. }
  405. return IRQ_NONE;
  406. }
  407. static int max44009_probe(struct i2c_client *client,
  408. const struct i2c_device_id *id)
  409. {
  410. struct max44009_data *data;
  411. struct iio_dev *indio_dev;
  412. int ret;
  413. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  414. if (!indio_dev)
  415. return -ENOMEM;
  416. data = iio_priv(indio_dev);
  417. i2c_set_clientdata(client, indio_dev);
  418. data->client = client;
  419. indio_dev->info = &max44009_info;
  420. indio_dev->modes = INDIO_DIRECT_MODE;
  421. indio_dev->name = MAX44009_DRV_NAME;
  422. indio_dev->channels = max44009_channels;
  423. indio_dev->num_channels = ARRAY_SIZE(max44009_channels);
  424. mutex_init(&data->lock);
  425. /* Clear any stale interrupt bit */
  426. ret = i2c_smbus_read_byte_data(client, MAX44009_REG_CFG);
  427. if (ret < 0)
  428. return ret;
  429. if (client->irq > 0) {
  430. ret = devm_request_threaded_irq(&client->dev, client->irq,
  431. NULL,
  432. max44009_threaded_irq_handler,
  433. IRQF_TRIGGER_FALLING |
  434. IRQF_ONESHOT | IRQF_SHARED,
  435. "max44009_event",
  436. indio_dev);
  437. if (ret < 0)
  438. return ret;
  439. }
  440. return devm_iio_device_register(&client->dev, indio_dev);
  441. }
  442. static const struct of_device_id max44009_of_match[] = {
  443. { .compatible = "maxim,max44009" },
  444. { }
  445. };
  446. MODULE_DEVICE_TABLE(of, max44009_of_match);
  447. static const struct i2c_device_id max44009_id[] = {
  448. { "max44009", 0 },
  449. { }
  450. };
  451. MODULE_DEVICE_TABLE(i2c, max44009_id);
  452. static struct i2c_driver max44009_driver = {
  453. .driver = {
  454. .name = MAX44009_DRV_NAME,
  455. .of_match_table = max44009_of_match,
  456. },
  457. .probe = max44009_probe,
  458. .id_table = max44009_id,
  459. };
  460. module_i2c_driver(max44009_driver);
  461. MODULE_AUTHOR("Robert Eshleman <[email protected]>");
  462. MODULE_LICENSE("GPL v2");
  463. MODULE_DESCRIPTION("MAX44009 ambient light sensor driver");