isl29028.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. /*
  2. * IIO driver for the light sensor ISL29028.
  3. * ISL29028 is Concurrent Ambient Light and Proximity Sensor
  4. *
  5. * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
  6. * Copyright (c) 2016-2017 Brian Masney <masneyb@onstation.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms and conditions of the GNU General Public License,
  10. * version 2, as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * Datasheets:
  21. * - http://www.intersil.com/content/dam/Intersil/documents/isl2/isl29028.pdf
  22. * - http://www.intersil.com/content/dam/Intersil/documents/isl2/isl29030.pdf
  23. */
  24. #include <linux/module.h>
  25. #include <linux/i2c.h>
  26. #include <linux/err.h>
  27. #include <linux/mutex.h>
  28. #include <linux/delay.h>
  29. #include <linux/slab.h>
  30. #include <linux/regmap.h>
  31. #include <linux/iio/iio.h>
  32. #include <linux/iio/sysfs.h>
  33. #include <linux/pm_runtime.h>
  34. #define ISL29028_CONV_TIME_MS 100
  35. #define ISL29028_REG_CONFIGURE 0x01
  36. #define ISL29028_CONF_ALS_IR_MODE_ALS 0
  37. #define ISL29028_CONF_ALS_IR_MODE_IR BIT(0)
  38. #define ISL29028_CONF_ALS_IR_MODE_MASK BIT(0)
  39. #define ISL29028_CONF_ALS_RANGE_LOW_LUX 0
  40. #define ISL29028_CONF_ALS_RANGE_HIGH_LUX BIT(1)
  41. #define ISL29028_CONF_ALS_RANGE_MASK BIT(1)
  42. #define ISL29028_CONF_ALS_DIS 0
  43. #define ISL29028_CONF_ALS_EN BIT(2)
  44. #define ISL29028_CONF_ALS_EN_MASK BIT(2)
  45. #define ISL29028_CONF_PROX_SLP_SH 4
  46. #define ISL29028_CONF_PROX_SLP_MASK (7 << ISL29028_CONF_PROX_SLP_SH)
  47. #define ISL29028_CONF_PROX_EN BIT(7)
  48. #define ISL29028_CONF_PROX_EN_MASK BIT(7)
  49. #define ISL29028_REG_INTERRUPT 0x02
  50. #define ISL29028_REG_PROX_DATA 0x08
  51. #define ISL29028_REG_ALSIR_L 0x09
  52. #define ISL29028_REG_ALSIR_U 0x0A
  53. #define ISL29028_REG_TEST1_MODE 0x0E
  54. #define ISL29028_REG_TEST2_MODE 0x0F
  55. #define ISL29028_NUM_REGS (ISL29028_REG_TEST2_MODE + 1)
  56. #define ISL29028_POWER_OFF_DELAY_MS 2000
  57. struct isl29028_prox_data {
  58. int sampling_int;
  59. int sampling_fract;
  60. int sleep_time;
  61. };
  62. static const struct isl29028_prox_data isl29028_prox_data[] = {
  63. { 1, 250000, 800 },
  64. { 2, 500000, 400 },
  65. { 5, 0, 200 },
  66. { 10, 0, 100 },
  67. { 13, 300000, 75 },
  68. { 20, 0, 50 },
  69. { 80, 0, 13 }, /*
  70. * Note: Data sheet lists 12.5 ms sleep time.
  71. * Round up a half millisecond for msleep().
  72. */
  73. { 100, 0, 0 }
  74. };
  75. enum isl29028_als_ir_mode {
  76. ISL29028_MODE_NONE = 0,
  77. ISL29028_MODE_ALS,
  78. ISL29028_MODE_IR,
  79. };
  80. struct isl29028_chip {
  81. struct mutex lock;
  82. struct regmap *regmap;
  83. int prox_sampling_int;
  84. int prox_sampling_frac;
  85. bool enable_prox;
  86. int lux_scale;
  87. enum isl29028_als_ir_mode als_ir_mode;
  88. };
  89. static int isl29028_find_prox_sleep_index(int sampling_int, int sampling_fract)
  90. {
  91. int i;
  92. for (i = 0; i < ARRAY_SIZE(isl29028_prox_data); ++i) {
  93. if (isl29028_prox_data[i].sampling_int == sampling_int &&
  94. isl29028_prox_data[i].sampling_fract == sampling_fract)
  95. return i;
  96. }
  97. return -EINVAL;
  98. }
  99. static int isl29028_set_proxim_sampling(struct isl29028_chip *chip,
  100. int sampling_int, int sampling_fract)
  101. {
  102. struct device *dev = regmap_get_device(chip->regmap);
  103. int sleep_index, ret;
  104. sleep_index = isl29028_find_prox_sleep_index(sampling_int,
  105. sampling_fract);
  106. if (sleep_index < 0)
  107. return sleep_index;
  108. ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
  109. ISL29028_CONF_PROX_SLP_MASK,
  110. sleep_index << ISL29028_CONF_PROX_SLP_SH);
  111. if (ret < 0) {
  112. dev_err(dev, "%s(): Error %d setting the proximity sampling\n",
  113. __func__, ret);
  114. return ret;
  115. }
  116. chip->prox_sampling_int = sampling_int;
  117. chip->prox_sampling_frac = sampling_fract;
  118. return ret;
  119. }
  120. static int isl29028_enable_proximity(struct isl29028_chip *chip)
  121. {
  122. int prox_index, ret;
  123. ret = isl29028_set_proxim_sampling(chip, chip->prox_sampling_int,
  124. chip->prox_sampling_frac);
  125. if (ret < 0)
  126. return ret;
  127. ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
  128. ISL29028_CONF_PROX_EN_MASK,
  129. ISL29028_CONF_PROX_EN);
  130. if (ret < 0)
  131. return ret;
  132. /* Wait for conversion to be complete for first sample */
  133. prox_index = isl29028_find_prox_sleep_index(chip->prox_sampling_int,
  134. chip->prox_sampling_frac);
  135. if (prox_index < 0)
  136. return prox_index;
  137. msleep(isl29028_prox_data[prox_index].sleep_time);
  138. return 0;
  139. }
  140. static int isl29028_set_als_scale(struct isl29028_chip *chip, int lux_scale)
  141. {
  142. struct device *dev = regmap_get_device(chip->regmap);
  143. int val = (lux_scale == 2000) ? ISL29028_CONF_ALS_RANGE_HIGH_LUX :
  144. ISL29028_CONF_ALS_RANGE_LOW_LUX;
  145. int ret;
  146. ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
  147. ISL29028_CONF_ALS_RANGE_MASK, val);
  148. if (ret < 0) {
  149. dev_err(dev, "%s(): Error %d setting the ALS scale\n", __func__,
  150. ret);
  151. return ret;
  152. }
  153. chip->lux_scale = lux_scale;
  154. return ret;
  155. }
  156. static int isl29028_set_als_ir_mode(struct isl29028_chip *chip,
  157. enum isl29028_als_ir_mode mode)
  158. {
  159. int ret;
  160. if (chip->als_ir_mode == mode)
  161. return 0;
  162. ret = isl29028_set_als_scale(chip, chip->lux_scale);
  163. if (ret < 0)
  164. return ret;
  165. switch (mode) {
  166. case ISL29028_MODE_ALS:
  167. ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
  168. ISL29028_CONF_ALS_IR_MODE_MASK,
  169. ISL29028_CONF_ALS_IR_MODE_ALS);
  170. if (ret < 0)
  171. return ret;
  172. ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
  173. ISL29028_CONF_ALS_RANGE_MASK,
  174. ISL29028_CONF_ALS_RANGE_HIGH_LUX);
  175. break;
  176. case ISL29028_MODE_IR:
  177. ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
  178. ISL29028_CONF_ALS_IR_MODE_MASK,
  179. ISL29028_CONF_ALS_IR_MODE_IR);
  180. break;
  181. case ISL29028_MODE_NONE:
  182. return regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
  183. ISL29028_CONF_ALS_EN_MASK,
  184. ISL29028_CONF_ALS_DIS);
  185. }
  186. if (ret < 0)
  187. return ret;
  188. /* Enable the ALS/IR */
  189. ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
  190. ISL29028_CONF_ALS_EN_MASK,
  191. ISL29028_CONF_ALS_EN);
  192. if (ret < 0)
  193. return ret;
  194. /* Need to wait for conversion time if ALS/IR mode enabled */
  195. msleep(ISL29028_CONV_TIME_MS);
  196. chip->als_ir_mode = mode;
  197. return 0;
  198. }
  199. static int isl29028_read_als_ir(struct isl29028_chip *chip, int *als_ir)
  200. {
  201. struct device *dev = regmap_get_device(chip->regmap);
  202. unsigned int lsb;
  203. unsigned int msb;
  204. int ret;
  205. ret = regmap_read(chip->regmap, ISL29028_REG_ALSIR_L, &lsb);
  206. if (ret < 0) {
  207. dev_err(dev,
  208. "%s(): Error %d reading register ALSIR_L\n",
  209. __func__, ret);
  210. return ret;
  211. }
  212. ret = regmap_read(chip->regmap, ISL29028_REG_ALSIR_U, &msb);
  213. if (ret < 0) {
  214. dev_err(dev,
  215. "%s(): Error %d reading register ALSIR_U\n",
  216. __func__, ret);
  217. return ret;
  218. }
  219. *als_ir = ((msb & 0xF) << 8) | (lsb & 0xFF);
  220. return 0;
  221. }
  222. static int isl29028_read_proxim(struct isl29028_chip *chip, int *prox)
  223. {
  224. struct device *dev = regmap_get_device(chip->regmap);
  225. unsigned int data;
  226. int ret;
  227. if (!chip->enable_prox) {
  228. ret = isl29028_enable_proximity(chip);
  229. if (ret < 0)
  230. return ret;
  231. chip->enable_prox = true;
  232. }
  233. ret = regmap_read(chip->regmap, ISL29028_REG_PROX_DATA, &data);
  234. if (ret < 0) {
  235. dev_err(dev, "%s(): Error %d reading register PROX_DATA\n",
  236. __func__, ret);
  237. return ret;
  238. }
  239. *prox = data;
  240. return 0;
  241. }
  242. static int isl29028_als_get(struct isl29028_chip *chip, int *als_data)
  243. {
  244. struct device *dev = regmap_get_device(chip->regmap);
  245. int ret;
  246. int als_ir_data;
  247. ret = isl29028_set_als_ir_mode(chip, ISL29028_MODE_ALS);
  248. if (ret < 0) {
  249. dev_err(dev, "%s(): Error %d enabling ALS mode\n", __func__,
  250. ret);
  251. return ret;
  252. }
  253. ret = isl29028_read_als_ir(chip, &als_ir_data);
  254. if (ret < 0)
  255. return ret;
  256. /*
  257. * convert als data count to lux.
  258. * if lux_scale = 125, lux = count * 0.031
  259. * if lux_scale = 2000, lux = count * 0.49
  260. */
  261. if (chip->lux_scale == 125)
  262. als_ir_data = (als_ir_data * 31) / 1000;
  263. else
  264. als_ir_data = (als_ir_data * 49) / 100;
  265. *als_data = als_ir_data;
  266. return 0;
  267. }
  268. static int isl29028_ir_get(struct isl29028_chip *chip, int *ir_data)
  269. {
  270. struct device *dev = regmap_get_device(chip->regmap);
  271. int ret;
  272. ret = isl29028_set_als_ir_mode(chip, ISL29028_MODE_IR);
  273. if (ret < 0) {
  274. dev_err(dev, "%s(): Error %d enabling IR mode\n", __func__,
  275. ret);
  276. return ret;
  277. }
  278. return isl29028_read_als_ir(chip, ir_data);
  279. }
  280. static int isl29028_set_pm_runtime_busy(struct isl29028_chip *chip, bool on)
  281. {
  282. struct device *dev = regmap_get_device(chip->regmap);
  283. int ret;
  284. if (on) {
  285. ret = pm_runtime_get_sync(dev);
  286. if (ret < 0)
  287. pm_runtime_put_noidle(dev);
  288. } else {
  289. pm_runtime_mark_last_busy(dev);
  290. ret = pm_runtime_put_autosuspend(dev);
  291. }
  292. return ret;
  293. }
  294. /* Channel IO */
  295. static int isl29028_write_raw(struct iio_dev *indio_dev,
  296. struct iio_chan_spec const *chan,
  297. int val, int val2, long mask)
  298. {
  299. struct isl29028_chip *chip = iio_priv(indio_dev);
  300. struct device *dev = regmap_get_device(chip->regmap);
  301. int ret;
  302. ret = isl29028_set_pm_runtime_busy(chip, true);
  303. if (ret < 0)
  304. return ret;
  305. mutex_lock(&chip->lock);
  306. ret = -EINVAL;
  307. switch (chan->type) {
  308. case IIO_PROXIMITY:
  309. if (mask != IIO_CHAN_INFO_SAMP_FREQ) {
  310. dev_err(dev,
  311. "%s(): proximity: Mask value 0x%08lx is not supported\n",
  312. __func__, mask);
  313. break;
  314. }
  315. if (val < 1 || val > 100) {
  316. dev_err(dev,
  317. "%s(): proximity: Sampling frequency %d is not in the range [1:100]\n",
  318. __func__, val);
  319. break;
  320. }
  321. ret = isl29028_set_proxim_sampling(chip, val, val2);
  322. break;
  323. case IIO_LIGHT:
  324. if (mask != IIO_CHAN_INFO_SCALE) {
  325. dev_err(dev,
  326. "%s(): light: Mask value 0x%08lx is not supported\n",
  327. __func__, mask);
  328. break;
  329. }
  330. if (val != 125 && val != 2000) {
  331. dev_err(dev,
  332. "%s(): light: Lux scale %d is not in the set {125, 2000}\n",
  333. __func__, val);
  334. break;
  335. }
  336. ret = isl29028_set_als_scale(chip, val);
  337. break;
  338. default:
  339. dev_err(dev, "%s(): Unsupported channel type %x\n",
  340. __func__, chan->type);
  341. break;
  342. }
  343. mutex_unlock(&chip->lock);
  344. if (ret < 0)
  345. return ret;
  346. ret = isl29028_set_pm_runtime_busy(chip, false);
  347. if (ret < 0)
  348. return ret;
  349. return ret;
  350. }
  351. static int isl29028_read_raw(struct iio_dev *indio_dev,
  352. struct iio_chan_spec const *chan,
  353. int *val, int *val2, long mask)
  354. {
  355. struct isl29028_chip *chip = iio_priv(indio_dev);
  356. struct device *dev = regmap_get_device(chip->regmap);
  357. int ret, pm_ret;
  358. ret = isl29028_set_pm_runtime_busy(chip, true);
  359. if (ret < 0)
  360. return ret;
  361. mutex_lock(&chip->lock);
  362. ret = -EINVAL;
  363. switch (mask) {
  364. case IIO_CHAN_INFO_RAW:
  365. case IIO_CHAN_INFO_PROCESSED:
  366. switch (chan->type) {
  367. case IIO_LIGHT:
  368. ret = isl29028_als_get(chip, val);
  369. break;
  370. case IIO_INTENSITY:
  371. ret = isl29028_ir_get(chip, val);
  372. break;
  373. case IIO_PROXIMITY:
  374. ret = isl29028_read_proxim(chip, val);
  375. break;
  376. default:
  377. break;
  378. }
  379. if (ret < 0)
  380. break;
  381. ret = IIO_VAL_INT;
  382. break;
  383. case IIO_CHAN_INFO_SAMP_FREQ:
  384. if (chan->type != IIO_PROXIMITY)
  385. break;
  386. *val = chip->prox_sampling_int;
  387. *val2 = chip->prox_sampling_frac;
  388. ret = IIO_VAL_INT;
  389. break;
  390. case IIO_CHAN_INFO_SCALE:
  391. if (chan->type != IIO_LIGHT)
  392. break;
  393. *val = chip->lux_scale;
  394. ret = IIO_VAL_INT;
  395. break;
  396. default:
  397. dev_err(dev, "%s(): mask value 0x%08lx is not supported\n",
  398. __func__, mask);
  399. break;
  400. }
  401. mutex_unlock(&chip->lock);
  402. if (ret < 0)
  403. return ret;
  404. /**
  405. * Preserve the ret variable if the call to
  406. * isl29028_set_pm_runtime_busy() is successful so the reading
  407. * (if applicable) is returned to user space.
  408. */
  409. pm_ret = isl29028_set_pm_runtime_busy(chip, false);
  410. if (pm_ret < 0)
  411. return pm_ret;
  412. return ret;
  413. }
  414. static IIO_CONST_ATTR(in_proximity_sampling_frequency_available,
  415. "1.25 2.5 5 10 13.3 20 80 100");
  416. static IIO_CONST_ATTR(in_illuminance_scale_available, "125 2000");
  417. #define ISL29028_CONST_ATTR(name) (&iio_const_attr_##name.dev_attr.attr)
  418. static struct attribute *isl29028_attributes[] = {
  419. ISL29028_CONST_ATTR(in_proximity_sampling_frequency_available),
  420. ISL29028_CONST_ATTR(in_illuminance_scale_available),
  421. NULL,
  422. };
  423. static const struct attribute_group isl29108_group = {
  424. .attrs = isl29028_attributes,
  425. };
  426. static const struct iio_chan_spec isl29028_channels[] = {
  427. {
  428. .type = IIO_LIGHT,
  429. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
  430. BIT(IIO_CHAN_INFO_SCALE),
  431. }, {
  432. .type = IIO_INTENSITY,
  433. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  434. }, {
  435. .type = IIO_PROXIMITY,
  436. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  437. BIT(IIO_CHAN_INFO_SAMP_FREQ),
  438. }
  439. };
  440. static const struct iio_info isl29028_info = {
  441. .attrs = &isl29108_group,
  442. .read_raw = isl29028_read_raw,
  443. .write_raw = isl29028_write_raw,
  444. };
  445. static int isl29028_clear_configure_reg(struct isl29028_chip *chip)
  446. {
  447. struct device *dev = regmap_get_device(chip->regmap);
  448. int ret;
  449. ret = regmap_write(chip->regmap, ISL29028_REG_CONFIGURE, 0x0);
  450. if (ret < 0)
  451. dev_err(dev, "%s(): Error %d clearing the CONFIGURE register\n",
  452. __func__, ret);
  453. chip->als_ir_mode = ISL29028_MODE_NONE;
  454. chip->enable_prox = false;
  455. return ret;
  456. }
  457. static bool isl29028_is_volatile_reg(struct device *dev, unsigned int reg)
  458. {
  459. switch (reg) {
  460. case ISL29028_REG_INTERRUPT:
  461. case ISL29028_REG_PROX_DATA:
  462. case ISL29028_REG_ALSIR_L:
  463. case ISL29028_REG_ALSIR_U:
  464. return true;
  465. default:
  466. return false;
  467. }
  468. }
  469. static const struct regmap_config isl29028_regmap_config = {
  470. .reg_bits = 8,
  471. .val_bits = 8,
  472. .volatile_reg = isl29028_is_volatile_reg,
  473. .max_register = ISL29028_NUM_REGS - 1,
  474. .num_reg_defaults_raw = ISL29028_NUM_REGS,
  475. .cache_type = REGCACHE_RBTREE,
  476. };
  477. static int isl29028_probe(struct i2c_client *client,
  478. const struct i2c_device_id *id)
  479. {
  480. struct isl29028_chip *chip;
  481. struct iio_dev *indio_dev;
  482. int ret;
  483. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
  484. if (!indio_dev)
  485. return -ENOMEM;
  486. chip = iio_priv(indio_dev);
  487. i2c_set_clientdata(client, indio_dev);
  488. mutex_init(&chip->lock);
  489. chip->regmap = devm_regmap_init_i2c(client, &isl29028_regmap_config);
  490. if (IS_ERR(chip->regmap)) {
  491. ret = PTR_ERR(chip->regmap);
  492. dev_err(&client->dev, "%s: Error %d initializing regmap\n",
  493. __func__, ret);
  494. return ret;
  495. }
  496. chip->enable_prox = false;
  497. chip->prox_sampling_int = 20;
  498. chip->prox_sampling_frac = 0;
  499. chip->lux_scale = 2000;
  500. ret = regmap_write(chip->regmap, ISL29028_REG_TEST1_MODE, 0x0);
  501. if (ret < 0) {
  502. dev_err(&client->dev,
  503. "%s(): Error %d writing to TEST1_MODE register\n",
  504. __func__, ret);
  505. return ret;
  506. }
  507. ret = regmap_write(chip->regmap, ISL29028_REG_TEST2_MODE, 0x0);
  508. if (ret < 0) {
  509. dev_err(&client->dev,
  510. "%s(): Error %d writing to TEST2_MODE register\n",
  511. __func__, ret);
  512. return ret;
  513. }
  514. ret = isl29028_clear_configure_reg(chip);
  515. if (ret < 0)
  516. return ret;
  517. indio_dev->info = &isl29028_info;
  518. indio_dev->channels = isl29028_channels;
  519. indio_dev->num_channels = ARRAY_SIZE(isl29028_channels);
  520. indio_dev->name = id->name;
  521. indio_dev->dev.parent = &client->dev;
  522. indio_dev->modes = INDIO_DIRECT_MODE;
  523. pm_runtime_enable(&client->dev);
  524. pm_runtime_set_autosuspend_delay(&client->dev,
  525. ISL29028_POWER_OFF_DELAY_MS);
  526. pm_runtime_use_autosuspend(&client->dev);
  527. ret = devm_iio_device_register(indio_dev->dev.parent, indio_dev);
  528. if (ret < 0) {
  529. dev_err(&client->dev,
  530. "%s(): iio registration failed with error %d\n",
  531. __func__, ret);
  532. return ret;
  533. }
  534. return 0;
  535. }
  536. static int isl29028_remove(struct i2c_client *client)
  537. {
  538. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  539. struct isl29028_chip *chip = iio_priv(indio_dev);
  540. iio_device_unregister(indio_dev);
  541. pm_runtime_disable(&client->dev);
  542. pm_runtime_set_suspended(&client->dev);
  543. pm_runtime_put_noidle(&client->dev);
  544. return isl29028_clear_configure_reg(chip);
  545. }
  546. static int __maybe_unused isl29028_suspend(struct device *dev)
  547. {
  548. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  549. struct isl29028_chip *chip = iio_priv(indio_dev);
  550. int ret;
  551. mutex_lock(&chip->lock);
  552. ret = isl29028_clear_configure_reg(chip);
  553. mutex_unlock(&chip->lock);
  554. return ret;
  555. }
  556. static int __maybe_unused isl29028_resume(struct device *dev)
  557. {
  558. /**
  559. * The specific component (ALS/IR or proximity) will enable itself as
  560. * needed the next time that the user requests a reading. This is done
  561. * above in isl29028_set_als_ir_mode() and isl29028_enable_proximity().
  562. */
  563. return 0;
  564. }
  565. static const struct dev_pm_ops isl29028_pm_ops = {
  566. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  567. pm_runtime_force_resume)
  568. SET_RUNTIME_PM_OPS(isl29028_suspend, isl29028_resume, NULL)
  569. };
  570. static const struct i2c_device_id isl29028_id[] = {
  571. {"isl29028", 0},
  572. {"isl29030", 0},
  573. {}
  574. };
  575. MODULE_DEVICE_TABLE(i2c, isl29028_id);
  576. static const struct of_device_id isl29028_of_match[] = {
  577. { .compatible = "isl,isl29028", }, /* for backward compat., don't use */
  578. { .compatible = "isil,isl29028", },
  579. { .compatible = "isil,isl29030", },
  580. { },
  581. };
  582. MODULE_DEVICE_TABLE(of, isl29028_of_match);
  583. static struct i2c_driver isl29028_driver = {
  584. .driver = {
  585. .name = "isl29028",
  586. .pm = &isl29028_pm_ops,
  587. .of_match_table = isl29028_of_match,
  588. },
  589. .probe = isl29028_probe,
  590. .remove = isl29028_remove,
  591. .id_table = isl29028_id,
  592. };
  593. module_i2c_driver(isl29028_driver);
  594. MODULE_DESCRIPTION("ISL29028 Ambient Light and Proximity Sensor driver");
  595. MODULE_LICENSE("GPL v2");
  596. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");