max44000.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. /*
  2. * MAX44000 Ambient and Infrared Proximity Sensor
  3. *
  4. * Copyright (c) 2016, Intel Corporation.
  5. *
  6. * This file is subject to the terms and conditions of version 2 of
  7. * the GNU General Public License. See the file COPYING in the main
  8. * directory of this archive for more details.
  9. *
  10. * Data sheet: https://datasheets.maximintegrated.com/en/ds/MAX44000.pdf
  11. *
  12. * 7-bit I2C slave address 0x4a
  13. */
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/i2c.h>
  17. #include <linux/regmap.h>
  18. #include <linux/util_macros.h>
  19. #include <linux/iio/iio.h>
  20. #include <linux/iio/sysfs.h>
  21. #include <linux/iio/buffer.h>
  22. #include <linux/iio/trigger_consumer.h>
  23. #include <linux/iio/triggered_buffer.h>
  24. #include <linux/acpi.h>
  25. #define MAX44000_DRV_NAME "max44000"
  26. /* Registers in datasheet order */
  27. #define MAX44000_REG_STATUS 0x00
  28. #define MAX44000_REG_CFG_MAIN 0x01
  29. #define MAX44000_REG_CFG_RX 0x02
  30. #define MAX44000_REG_CFG_TX 0x03
  31. #define MAX44000_REG_ALS_DATA_HI 0x04
  32. #define MAX44000_REG_ALS_DATA_LO 0x05
  33. #define MAX44000_REG_PRX_DATA 0x16
  34. #define MAX44000_REG_ALS_UPTHR_HI 0x06
  35. #define MAX44000_REG_ALS_UPTHR_LO 0x07
  36. #define MAX44000_REG_ALS_LOTHR_HI 0x08
  37. #define MAX44000_REG_ALS_LOTHR_LO 0x09
  38. #define MAX44000_REG_PST 0x0a
  39. #define MAX44000_REG_PRX_IND 0x0b
  40. #define MAX44000_REG_PRX_THR 0x0c
  41. #define MAX44000_REG_TRIM_GAIN_GREEN 0x0f
  42. #define MAX44000_REG_TRIM_GAIN_IR 0x10
  43. /* REG_CFG bits */
  44. #define MAX44000_CFG_ALSINTE 0x01
  45. #define MAX44000_CFG_PRXINTE 0x02
  46. #define MAX44000_CFG_MASK 0x1c
  47. #define MAX44000_CFG_MODE_SHUTDOWN 0x00
  48. #define MAX44000_CFG_MODE_ALS_GIR 0x04
  49. #define MAX44000_CFG_MODE_ALS_G 0x08
  50. #define MAX44000_CFG_MODE_ALS_IR 0x0c
  51. #define MAX44000_CFG_MODE_ALS_PRX 0x10
  52. #define MAX44000_CFG_MODE_PRX 0x14
  53. #define MAX44000_CFG_TRIM 0x20
  54. /*
  55. * Upper 4 bits are not documented but start as 1 on powerup
  56. * Setting them to 0 causes proximity to misbehave so set them to 1
  57. */
  58. #define MAX44000_REG_CFG_RX_DEFAULT 0xf0
  59. /* REG_RX bits */
  60. #define MAX44000_CFG_RX_ALSTIM_MASK 0x0c
  61. #define MAX44000_CFG_RX_ALSTIM_SHIFT 2
  62. #define MAX44000_CFG_RX_ALSPGA_MASK 0x03
  63. #define MAX44000_CFG_RX_ALSPGA_SHIFT 0
  64. /* REG_TX bits */
  65. #define MAX44000_LED_CURRENT_MASK 0xf
  66. #define MAX44000_LED_CURRENT_MAX 11
  67. #define MAX44000_LED_CURRENT_DEFAULT 6
  68. #define MAX44000_ALSDATA_OVERFLOW 0x4000
  69. struct max44000_data {
  70. struct mutex lock;
  71. struct regmap *regmap;
  72. };
  73. /* Default scale is set to the minimum of 0.03125 or 1 / (1 << 5) lux */
  74. #define MAX44000_ALS_TO_LUX_DEFAULT_FRACTION_LOG2 5
  75. /* Scale can be multiplied by up to 128x via ALSPGA for measurement gain */
  76. static const int max44000_alspga_shift[] = {0, 2, 4, 7};
  77. #define MAX44000_ALSPGA_MAX_SHIFT 7
  78. /*
  79. * Scale can be multiplied by up to 64x via ALSTIM because of lost resolution
  80. *
  81. * This scaling factor is hidden from userspace and instead accounted for when
  82. * reading raw values from the device.
  83. *
  84. * This makes it possible to cleanly expose ALSPGA as IIO_CHAN_INFO_SCALE and
  85. * ALSTIM as IIO_CHAN_INFO_INT_TIME without the values affecting each other.
  86. *
  87. * Handling this internally is also required for buffer support because the
  88. * channel's scan_type can't be modified dynamically.
  89. */
  90. static const int max44000_alstim_shift[] = {0, 2, 4, 6};
  91. #define MAX44000_ALSTIM_SHIFT(alstim) (2 * (alstim))
  92. /* Available integration times with pretty manual alignment: */
  93. static const int max44000_int_time_avail_ns_array[] = {
  94. 100000000,
  95. 25000000,
  96. 6250000,
  97. 1562500,
  98. };
  99. static const char max44000_int_time_avail_str[] =
  100. "0.100 "
  101. "0.025 "
  102. "0.00625 "
  103. "0.0015625";
  104. /* Available scales (internal to ulux) with pretty manual alignment: */
  105. static const int max44000_scale_avail_ulux_array[] = {
  106. 31250,
  107. 125000,
  108. 500000,
  109. 4000000,
  110. };
  111. static const char max44000_scale_avail_str[] =
  112. "0.03125 "
  113. "0.125 "
  114. "0.5 "
  115. "4";
  116. #define MAX44000_SCAN_INDEX_ALS 0
  117. #define MAX44000_SCAN_INDEX_PRX 1
  118. static const struct iio_chan_spec max44000_channels[] = {
  119. {
  120. .type = IIO_LIGHT,
  121. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  122. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |
  123. BIT(IIO_CHAN_INFO_INT_TIME),
  124. .scan_index = MAX44000_SCAN_INDEX_ALS,
  125. .scan_type = {
  126. .sign = 'u',
  127. .realbits = 14,
  128. .storagebits = 16,
  129. }
  130. },
  131. {
  132. .type = IIO_PROXIMITY,
  133. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  134. .scan_index = MAX44000_SCAN_INDEX_PRX,
  135. .scan_type = {
  136. .sign = 'u',
  137. .realbits = 8,
  138. .storagebits = 16,
  139. }
  140. },
  141. IIO_CHAN_SOFT_TIMESTAMP(2),
  142. {
  143. .type = IIO_CURRENT,
  144. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  145. BIT(IIO_CHAN_INFO_SCALE),
  146. .extend_name = "led",
  147. .output = 1,
  148. .scan_index = -1,
  149. },
  150. };
  151. static int max44000_read_alstim(struct max44000_data *data)
  152. {
  153. unsigned int val;
  154. int ret;
  155. ret = regmap_read(data->regmap, MAX44000_REG_CFG_RX, &val);
  156. if (ret < 0)
  157. return ret;
  158. return (val & MAX44000_CFG_RX_ALSTIM_MASK) >> MAX44000_CFG_RX_ALSTIM_SHIFT;
  159. }
  160. static int max44000_write_alstim(struct max44000_data *data, int val)
  161. {
  162. return regmap_write_bits(data->regmap, MAX44000_REG_CFG_RX,
  163. MAX44000_CFG_RX_ALSTIM_MASK,
  164. val << MAX44000_CFG_RX_ALSTIM_SHIFT);
  165. }
  166. static int max44000_read_alspga(struct max44000_data *data)
  167. {
  168. unsigned int val;
  169. int ret;
  170. ret = regmap_read(data->regmap, MAX44000_REG_CFG_RX, &val);
  171. if (ret < 0)
  172. return ret;
  173. return (val & MAX44000_CFG_RX_ALSPGA_MASK) >> MAX44000_CFG_RX_ALSPGA_SHIFT;
  174. }
  175. static int max44000_write_alspga(struct max44000_data *data, int val)
  176. {
  177. return regmap_write_bits(data->regmap, MAX44000_REG_CFG_RX,
  178. MAX44000_CFG_RX_ALSPGA_MASK,
  179. val << MAX44000_CFG_RX_ALSPGA_SHIFT);
  180. }
  181. static int max44000_read_alsval(struct max44000_data *data)
  182. {
  183. u16 regval;
  184. int alstim, ret;
  185. ret = regmap_bulk_read(data->regmap, MAX44000_REG_ALS_DATA_HI,
  186. &regval, sizeof(regval));
  187. if (ret < 0)
  188. return ret;
  189. alstim = ret = max44000_read_alstim(data);
  190. if (ret < 0)
  191. return ret;
  192. regval = be16_to_cpu(regval);
  193. /*
  194. * Overflow is explained on datasheet page 17.
  195. *
  196. * It's a warning that either the G or IR channel has become saturated
  197. * and that the value in the register is likely incorrect.
  198. *
  199. * The recommendation is to change the scale (ALSPGA).
  200. * The driver just returns the max representable value.
  201. */
  202. if (regval & MAX44000_ALSDATA_OVERFLOW)
  203. return 0x3FFF;
  204. return regval << MAX44000_ALSTIM_SHIFT(alstim);
  205. }
  206. static int max44000_write_led_current_raw(struct max44000_data *data, int val)
  207. {
  208. /* Maybe we should clamp the value instead? */
  209. if (val < 0 || val > MAX44000_LED_CURRENT_MAX)
  210. return -ERANGE;
  211. if (val >= 8)
  212. val += 4;
  213. return regmap_write_bits(data->regmap, MAX44000_REG_CFG_TX,
  214. MAX44000_LED_CURRENT_MASK, val);
  215. }
  216. static int max44000_read_led_current_raw(struct max44000_data *data)
  217. {
  218. unsigned int regval;
  219. int ret;
  220. ret = regmap_read(data->regmap, MAX44000_REG_CFG_TX, &regval);
  221. if (ret < 0)
  222. return ret;
  223. regval &= MAX44000_LED_CURRENT_MASK;
  224. if (regval >= 8)
  225. regval -= 4;
  226. return regval;
  227. }
  228. static int max44000_read_raw(struct iio_dev *indio_dev,
  229. struct iio_chan_spec const *chan,
  230. int *val, int *val2, long mask)
  231. {
  232. struct max44000_data *data = iio_priv(indio_dev);
  233. int alstim, alspga;
  234. unsigned int regval;
  235. int ret;
  236. switch (mask) {
  237. case IIO_CHAN_INFO_RAW:
  238. switch (chan->type) {
  239. case IIO_LIGHT:
  240. mutex_lock(&data->lock);
  241. ret = max44000_read_alsval(data);
  242. mutex_unlock(&data->lock);
  243. if (ret < 0)
  244. return ret;
  245. *val = ret;
  246. return IIO_VAL_INT;
  247. case IIO_PROXIMITY:
  248. mutex_lock(&data->lock);
  249. ret = regmap_read(data->regmap, MAX44000_REG_PRX_DATA, &regval);
  250. mutex_unlock(&data->lock);
  251. if (ret < 0)
  252. return ret;
  253. *val = regval;
  254. return IIO_VAL_INT;
  255. case IIO_CURRENT:
  256. mutex_lock(&data->lock);
  257. ret = max44000_read_led_current_raw(data);
  258. mutex_unlock(&data->lock);
  259. if (ret < 0)
  260. return ret;
  261. *val = ret;
  262. return IIO_VAL_INT;
  263. default:
  264. return -EINVAL;
  265. }
  266. case IIO_CHAN_INFO_SCALE:
  267. switch (chan->type) {
  268. case IIO_CURRENT:
  269. /* Output register is in 10s of miliamps */
  270. *val = 10;
  271. return IIO_VAL_INT;
  272. case IIO_LIGHT:
  273. mutex_lock(&data->lock);
  274. alspga = ret = max44000_read_alspga(data);
  275. mutex_unlock(&data->lock);
  276. if (ret < 0)
  277. return ret;
  278. /* Avoid negative shifts */
  279. *val = (1 << MAX44000_ALSPGA_MAX_SHIFT);
  280. *val2 = MAX44000_ALS_TO_LUX_DEFAULT_FRACTION_LOG2
  281. + MAX44000_ALSPGA_MAX_SHIFT
  282. - max44000_alspga_shift[alspga];
  283. return IIO_VAL_FRACTIONAL_LOG2;
  284. default:
  285. return -EINVAL;
  286. }
  287. case IIO_CHAN_INFO_INT_TIME:
  288. mutex_lock(&data->lock);
  289. alstim = ret = max44000_read_alstim(data);
  290. mutex_unlock(&data->lock);
  291. if (ret < 0)
  292. return ret;
  293. *val = 0;
  294. *val2 = max44000_int_time_avail_ns_array[alstim];
  295. return IIO_VAL_INT_PLUS_NANO;
  296. default:
  297. return -EINVAL;
  298. }
  299. }
  300. static int max44000_write_raw(struct iio_dev *indio_dev,
  301. struct iio_chan_spec const *chan,
  302. int val, int val2, long mask)
  303. {
  304. struct max44000_data *data = iio_priv(indio_dev);
  305. int ret;
  306. if (mask == IIO_CHAN_INFO_RAW && chan->type == IIO_CURRENT) {
  307. mutex_lock(&data->lock);
  308. ret = max44000_write_led_current_raw(data, val);
  309. mutex_unlock(&data->lock);
  310. return ret;
  311. } else if (mask == IIO_CHAN_INFO_INT_TIME && chan->type == IIO_LIGHT) {
  312. s64 valns = val * NSEC_PER_SEC + val2;
  313. int alstim = find_closest_descending(valns,
  314. max44000_int_time_avail_ns_array,
  315. ARRAY_SIZE(max44000_int_time_avail_ns_array));
  316. mutex_lock(&data->lock);
  317. ret = max44000_write_alstim(data, alstim);
  318. mutex_unlock(&data->lock);
  319. return ret;
  320. } else if (mask == IIO_CHAN_INFO_SCALE && chan->type == IIO_LIGHT) {
  321. s64 valus = val * USEC_PER_SEC + val2;
  322. int alspga = find_closest(valus,
  323. max44000_scale_avail_ulux_array,
  324. ARRAY_SIZE(max44000_scale_avail_ulux_array));
  325. mutex_lock(&data->lock);
  326. ret = max44000_write_alspga(data, alspga);
  327. mutex_unlock(&data->lock);
  328. return ret;
  329. }
  330. return -EINVAL;
  331. }
  332. static int max44000_write_raw_get_fmt(struct iio_dev *indio_dev,
  333. struct iio_chan_spec const *chan,
  334. long mask)
  335. {
  336. if (mask == IIO_CHAN_INFO_INT_TIME && chan->type == IIO_LIGHT)
  337. return IIO_VAL_INT_PLUS_NANO;
  338. else if (mask == IIO_CHAN_INFO_SCALE && chan->type == IIO_LIGHT)
  339. return IIO_VAL_INT_PLUS_MICRO;
  340. else
  341. return IIO_VAL_INT;
  342. }
  343. static IIO_CONST_ATTR(illuminance_integration_time_available, max44000_int_time_avail_str);
  344. static IIO_CONST_ATTR(illuminance_scale_available, max44000_scale_avail_str);
  345. static struct attribute *max44000_attributes[] = {
  346. &iio_const_attr_illuminance_integration_time_available.dev_attr.attr,
  347. &iio_const_attr_illuminance_scale_available.dev_attr.attr,
  348. NULL
  349. };
  350. static const struct attribute_group max44000_attribute_group = {
  351. .attrs = max44000_attributes,
  352. };
  353. static const struct iio_info max44000_info = {
  354. .driver_module = THIS_MODULE,
  355. .read_raw = max44000_read_raw,
  356. .write_raw = max44000_write_raw,
  357. .write_raw_get_fmt = max44000_write_raw_get_fmt,
  358. .attrs = &max44000_attribute_group,
  359. };
  360. static bool max44000_readable_reg(struct device *dev, unsigned int reg)
  361. {
  362. switch (reg) {
  363. case MAX44000_REG_STATUS:
  364. case MAX44000_REG_CFG_MAIN:
  365. case MAX44000_REG_CFG_RX:
  366. case MAX44000_REG_CFG_TX:
  367. case MAX44000_REG_ALS_DATA_HI:
  368. case MAX44000_REG_ALS_DATA_LO:
  369. case MAX44000_REG_PRX_DATA:
  370. case MAX44000_REG_ALS_UPTHR_HI:
  371. case MAX44000_REG_ALS_UPTHR_LO:
  372. case MAX44000_REG_ALS_LOTHR_HI:
  373. case MAX44000_REG_ALS_LOTHR_LO:
  374. case MAX44000_REG_PST:
  375. case MAX44000_REG_PRX_IND:
  376. case MAX44000_REG_PRX_THR:
  377. case MAX44000_REG_TRIM_GAIN_GREEN:
  378. case MAX44000_REG_TRIM_GAIN_IR:
  379. return true;
  380. default:
  381. return false;
  382. }
  383. }
  384. static bool max44000_writeable_reg(struct device *dev, unsigned int reg)
  385. {
  386. switch (reg) {
  387. case MAX44000_REG_CFG_MAIN:
  388. case MAX44000_REG_CFG_RX:
  389. case MAX44000_REG_CFG_TX:
  390. case MAX44000_REG_ALS_UPTHR_HI:
  391. case MAX44000_REG_ALS_UPTHR_LO:
  392. case MAX44000_REG_ALS_LOTHR_HI:
  393. case MAX44000_REG_ALS_LOTHR_LO:
  394. case MAX44000_REG_PST:
  395. case MAX44000_REG_PRX_IND:
  396. case MAX44000_REG_PRX_THR:
  397. case MAX44000_REG_TRIM_GAIN_GREEN:
  398. case MAX44000_REG_TRIM_GAIN_IR:
  399. return true;
  400. default:
  401. return false;
  402. }
  403. }
  404. static bool max44000_volatile_reg(struct device *dev, unsigned int reg)
  405. {
  406. switch (reg) {
  407. case MAX44000_REG_STATUS:
  408. case MAX44000_REG_ALS_DATA_HI:
  409. case MAX44000_REG_ALS_DATA_LO:
  410. case MAX44000_REG_PRX_DATA:
  411. return true;
  412. default:
  413. return false;
  414. }
  415. }
  416. static bool max44000_precious_reg(struct device *dev, unsigned int reg)
  417. {
  418. return reg == MAX44000_REG_STATUS;
  419. }
  420. static const struct regmap_config max44000_regmap_config = {
  421. .reg_bits = 8,
  422. .val_bits = 8,
  423. .max_register = MAX44000_REG_PRX_DATA,
  424. .readable_reg = max44000_readable_reg,
  425. .writeable_reg = max44000_writeable_reg,
  426. .volatile_reg = max44000_volatile_reg,
  427. .precious_reg = max44000_precious_reg,
  428. .use_single_rw = 1,
  429. .cache_type = REGCACHE_RBTREE,
  430. };
  431. static irqreturn_t max44000_trigger_handler(int irq, void *p)
  432. {
  433. struct iio_poll_func *pf = p;
  434. struct iio_dev *indio_dev = pf->indio_dev;
  435. struct max44000_data *data = iio_priv(indio_dev);
  436. u16 buf[8]; /* 2x u16 + padding + 8 bytes timestamp */
  437. int index = 0;
  438. unsigned int regval;
  439. int ret;
  440. mutex_lock(&data->lock);
  441. if (test_bit(MAX44000_SCAN_INDEX_ALS, indio_dev->active_scan_mask)) {
  442. ret = max44000_read_alsval(data);
  443. if (ret < 0)
  444. goto out_unlock;
  445. buf[index++] = ret;
  446. }
  447. if (test_bit(MAX44000_SCAN_INDEX_PRX, indio_dev->active_scan_mask)) {
  448. ret = regmap_read(data->regmap, MAX44000_REG_PRX_DATA, &regval);
  449. if (ret < 0)
  450. goto out_unlock;
  451. buf[index] = regval;
  452. }
  453. mutex_unlock(&data->lock);
  454. iio_push_to_buffers_with_timestamp(indio_dev, buf,
  455. iio_get_time_ns(indio_dev));
  456. iio_trigger_notify_done(indio_dev->trig);
  457. return IRQ_HANDLED;
  458. out_unlock:
  459. mutex_unlock(&data->lock);
  460. iio_trigger_notify_done(indio_dev->trig);
  461. return IRQ_HANDLED;
  462. }
  463. static int max44000_probe(struct i2c_client *client,
  464. const struct i2c_device_id *id)
  465. {
  466. struct max44000_data *data;
  467. struct iio_dev *indio_dev;
  468. int ret, reg;
  469. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  470. if (!indio_dev)
  471. return -ENOMEM;
  472. data = iio_priv(indio_dev);
  473. data->regmap = devm_regmap_init_i2c(client, &max44000_regmap_config);
  474. if (IS_ERR(data->regmap)) {
  475. dev_err(&client->dev, "regmap_init failed!\n");
  476. return PTR_ERR(data->regmap);
  477. }
  478. i2c_set_clientdata(client, indio_dev);
  479. mutex_init(&data->lock);
  480. indio_dev->dev.parent = &client->dev;
  481. indio_dev->info = &max44000_info;
  482. indio_dev->name = MAX44000_DRV_NAME;
  483. indio_dev->channels = max44000_channels;
  484. indio_dev->num_channels = ARRAY_SIZE(max44000_channels);
  485. /*
  486. * The device doesn't have a reset function so we just clear some
  487. * important bits at probe time to ensure sane operation.
  488. *
  489. * Since we don't support interrupts/events the threshold values are
  490. * not important. We also don't touch trim values.
  491. */
  492. /* Reset ALS scaling bits */
  493. ret = regmap_write(data->regmap, MAX44000_REG_CFG_RX,
  494. MAX44000_REG_CFG_RX_DEFAULT);
  495. if (ret < 0) {
  496. dev_err(&client->dev, "failed to write default CFG_RX: %d\n",
  497. ret);
  498. return ret;
  499. }
  500. /*
  501. * By default the LED pulse used for the proximity sensor is disabled.
  502. * Set a middle value so that we get some sort of valid data by default.
  503. */
  504. ret = max44000_write_led_current_raw(data, MAX44000_LED_CURRENT_DEFAULT);
  505. if (ret < 0) {
  506. dev_err(&client->dev, "failed to write init config: %d\n", ret);
  507. return ret;
  508. }
  509. /* Reset CFG bits to ALS_PRX mode which allows easy reading of both values. */
  510. reg = MAX44000_CFG_TRIM | MAX44000_CFG_MODE_ALS_PRX;
  511. ret = regmap_write(data->regmap, MAX44000_REG_CFG_MAIN, reg);
  512. if (ret < 0) {
  513. dev_err(&client->dev, "failed to write init config: %d\n", ret);
  514. return ret;
  515. }
  516. /* Read status at least once to clear any stale interrupt bits. */
  517. ret = regmap_read(data->regmap, MAX44000_REG_STATUS, &reg);
  518. if (ret < 0) {
  519. dev_err(&client->dev, "failed to read init status: %d\n", ret);
  520. return ret;
  521. }
  522. ret = iio_triggered_buffer_setup(indio_dev, NULL, max44000_trigger_handler, NULL);
  523. if (ret < 0) {
  524. dev_err(&client->dev, "iio triggered buffer setup failed\n");
  525. return ret;
  526. }
  527. return iio_device_register(indio_dev);
  528. }
  529. static int max44000_remove(struct i2c_client *client)
  530. {
  531. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  532. iio_device_unregister(indio_dev);
  533. iio_triggered_buffer_cleanup(indio_dev);
  534. return 0;
  535. }
  536. static const struct i2c_device_id max44000_id[] = {
  537. {"max44000", 0},
  538. { }
  539. };
  540. MODULE_DEVICE_TABLE(i2c, max44000_id);
  541. #ifdef CONFIG_ACPI
  542. static const struct acpi_device_id max44000_acpi_match[] = {
  543. {"MAX44000", 0},
  544. { }
  545. };
  546. MODULE_DEVICE_TABLE(acpi, max44000_acpi_match);
  547. #endif
  548. static struct i2c_driver max44000_driver = {
  549. .driver = {
  550. .name = MAX44000_DRV_NAME,
  551. .acpi_match_table = ACPI_PTR(max44000_acpi_match),
  552. },
  553. .probe = max44000_probe,
  554. .remove = max44000_remove,
  555. .id_table = max44000_id,
  556. };
  557. module_i2c_driver(max44000_driver);
  558. MODULE_AUTHOR("Crestez Dan Leonard <leonard.crestez@intel.com>");
  559. MODULE_DESCRIPTION("MAX44000 Ambient and Infrared Proximity Sensor");
  560. MODULE_LICENSE("GPL v2");