gp2ap002.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * These are the two Sharp GP2AP002 variants supported by this driver:
  4. * GP2AP002A00F Ambient Light and Proximity Sensor
  5. * GP2AP002S00F Proximity Sensor
  6. *
  7. * Copyright (C) 2020 Linaro Ltd.
  8. * Author: Linus Walleij <linus.walleij@linaro.org>
  9. *
  10. * Based partly on the code in Sony Ericssons GP2AP00200F driver by
  11. * Courtney Cavin and Oskar Andero in drivers/input/misc/gp2ap002a00f.c
  12. * Based partly on a Samsung misc driver submitted by
  13. * Donggeun Kim & Minkyu Kang in 2011:
  14. * https://lore.kernel.org/lkml/1315556546-7445-1-git-send-email-dg77.kim@samsung.com/
  15. * Based partly on a submission by
  16. * Jonathan Bakker and Paweł Chmiel in january 2019:
  17. * https://lore.kernel.org/linux-input/20190125175045.22576-1-pawel.mikolaj.chmiel@gmail.com/
  18. * Based partly on code from the Samsung GT-S7710 by <mjchen@sta.samsung.com>
  19. * Based partly on the code in LG Electronics GP2AP00200F driver by
  20. * Kenobi Lee <sungyoung.lee@lge.com> and EunYoung Cho <ey.cho@lge.com>
  21. */
  22. #include <linux/module.h>
  23. #include <linux/i2c.h>
  24. #include <linux/regmap.h>
  25. #include <linux/iio/iio.h>
  26. #include <linux/iio/sysfs.h>
  27. #include <linux/iio/events.h>
  28. #include <linux/iio/consumer.h> /* To get our ADC channel */
  29. #include <linux/iio/types.h> /* To deal with our ADC channel */
  30. #include <linux/init.h>
  31. #include <linux/delay.h>
  32. #include <linux/regulator/consumer.h>
  33. #include <linux/pm_runtime.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/bits.h>
  36. #include <linux/math64.h>
  37. #include <linux/pm.h>
  38. #define GP2AP002_PROX_CHANNEL 0
  39. #define GP2AP002_ALS_CHANNEL 1
  40. /* ------------------------------------------------------------------------ */
  41. /* ADDRESS SYMBOL DATA Init R/W */
  42. /* D7 D6 D5 D4 D3 D2 D1 D0 */
  43. /* ------------------------------------------------------------------------ */
  44. /* 0 PROX X X X X X X X VO H'00 R */
  45. /* 1 GAIN X X X X LED0 X X X H'00 W */
  46. /* 2 HYS HYSD HYSC1 HYSC0 X HYSF3 HYSF2 HYSF1 HYSF0 H'00 W */
  47. /* 3 CYCLE X X CYCL2 CYCL1 CYCL0 OSC2 X X H'00 W */
  48. /* 4 OPMOD X X X ASD X X VCON SSD H'00 W */
  49. /* 6 CON X X X OCON1 OCON0 X X X H'00 W */
  50. /* ------------------------------------------------------------------------ */
  51. /* VO :Proximity sensing result(0: no detection, 1: detection) */
  52. /* LED0 :Select switch for LED driver's On-registence(0:2x higher, 1:normal)*/
  53. /* HYSD/HYSF :Adjusts the receiver sensitivity */
  54. /* OSC :Select switch internal clocl frequency hoppling(0:effective) */
  55. /* CYCL :Determine the detection cycle(typically 8ms, up to 128x) */
  56. /* SSD :Software Shutdown function(0:shutdown, 1:operating) */
  57. /* VCON :VOUT output method control(0:normal, 1:interrupt) */
  58. /* ASD :Select switch for analog sleep function(0:ineffective, 1:effective)*/
  59. /* OCON :Select switch for enabling/disabling VOUT (00:enable, 11:disable) */
  60. #define GP2AP002_PROX 0x00
  61. #define GP2AP002_GAIN 0x01
  62. #define GP2AP002_HYS 0x02
  63. #define GP2AP002_CYCLE 0x03
  64. #define GP2AP002_OPMOD 0x04
  65. #define GP2AP002_CON 0x06
  66. #define GP2AP002_PROX_VO_DETECT BIT(0)
  67. /* Setting this bit to 0 means 2x higher LED resistance */
  68. #define GP2AP002_GAIN_LED_NORMAL BIT(3)
  69. /*
  70. * These bits adjusts the proximity sensitivity, determining characteristics
  71. * of the detection distance and its hysteresis.
  72. */
  73. #define GP2AP002_HYS_HYSD_SHIFT 7
  74. #define GP2AP002_HYS_HYSD_MASK BIT(7)
  75. #define GP2AP002_HYS_HYSC_SHIFT 5
  76. #define GP2AP002_HYS_HYSC_MASK GENMASK(6, 5)
  77. #define GP2AP002_HYS_HYSF_SHIFT 0
  78. #define GP2AP002_HYS_HYSF_MASK GENMASK(3, 0)
  79. #define GP2AP002_HYS_MASK (GP2AP002_HYS_HYSD_MASK | \
  80. GP2AP002_HYS_HYSC_MASK | \
  81. GP2AP002_HYS_HYSF_MASK)
  82. /*
  83. * These values determine the detection cycle response time
  84. * 0: 8ms, 1: 16ms, 2: 32ms, 3: 64ms, 4: 128ms,
  85. * 5: 256ms, 6: 512ms, 7: 1024ms
  86. */
  87. #define GP2AP002_CYCLE_CYCL_SHIFT 3
  88. #define GP2AP002_CYCLE_CYCL_MASK GENMASK(5, 3)
  89. /*
  90. * Select switch for internal clock frequency hopping
  91. * 0: effective,
  92. * 1: ineffective
  93. */
  94. #define GP2AP002_CYCLE_OSC_EFFECTIVE 0
  95. #define GP2AP002_CYCLE_OSC_INEFFECTIVE BIT(2)
  96. #define GP2AP002_CYCLE_OSC_MASK BIT(2)
  97. /* Analog sleep effective */
  98. #define GP2AP002_OPMOD_ASD BIT(4)
  99. /* Enable chip */
  100. #define GP2AP002_OPMOD_SSD_OPERATING BIT(0)
  101. /* IRQ mode */
  102. #define GP2AP002_OPMOD_VCON_IRQ BIT(1)
  103. #define GP2AP002_OPMOD_MASK (BIT(0) | BIT(1) | BIT(4))
  104. /*
  105. * Select switch for enabling/disabling Vout pin
  106. * 0: enable
  107. * 2: force to go Low
  108. * 3: force to go High
  109. */
  110. #define GP2AP002_CON_OCON_SHIFT 3
  111. #define GP2AP002_CON_OCON_ENABLE (0x0 << GP2AP002_CON_OCON_SHIFT)
  112. #define GP2AP002_CON_OCON_LOW (0x2 << GP2AP002_CON_OCON_SHIFT)
  113. #define GP2AP002_CON_OCON_HIGH (0x3 << GP2AP002_CON_OCON_SHIFT)
  114. #define GP2AP002_CON_OCON_MASK (0x3 << GP2AP002_CON_OCON_SHIFT)
  115. /**
  116. * struct gp2ap002 - GP2AP002 state
  117. * @map: regmap pointer for the i2c regmap
  118. * @dev: pointer to parent device
  119. * @vdd: regulator controlling VDD
  120. * @vio: regulator controlling VIO
  121. * @alsout: IIO ADC channel to convert the ALSOUT signal
  122. * @hys_far: hysteresis control from device tree
  123. * @hys_close: hysteresis control from device tree
  124. * @is_gp2ap002s00f: this is the GP2AP002F variant of the chip
  125. * @irq: the IRQ line used by this device
  126. * @enabled: we cannot read the status of the hardware so we need to
  127. * keep track of whether the event is enabled using this state variable
  128. */
  129. struct gp2ap002 {
  130. struct regmap *map;
  131. struct device *dev;
  132. struct regulator *vdd;
  133. struct regulator *vio;
  134. struct iio_channel *alsout;
  135. u8 hys_far;
  136. u8 hys_close;
  137. bool is_gp2ap002s00f;
  138. int irq;
  139. bool enabled;
  140. };
  141. static irqreturn_t gp2ap002_prox_irq(int irq, void *d)
  142. {
  143. struct iio_dev *indio_dev = d;
  144. struct gp2ap002 *gp2ap002 = iio_priv(indio_dev);
  145. u64 ev;
  146. int val;
  147. int ret;
  148. if (!gp2ap002->enabled)
  149. goto err_retrig;
  150. ret = regmap_read(gp2ap002->map, GP2AP002_PROX, &val);
  151. if (ret) {
  152. dev_err(gp2ap002->dev, "error reading proximity\n");
  153. goto err_retrig;
  154. }
  155. if (val & GP2AP002_PROX_VO_DETECT) {
  156. /* Close */
  157. dev_dbg(gp2ap002->dev, "close\n");
  158. ret = regmap_write(gp2ap002->map, GP2AP002_HYS,
  159. gp2ap002->hys_far);
  160. if (ret)
  161. dev_err(gp2ap002->dev,
  162. "error setting up proximity hysteresis\n");
  163. ev = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, GP2AP002_PROX_CHANNEL,
  164. IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING);
  165. } else {
  166. /* Far */
  167. dev_dbg(gp2ap002->dev, "far\n");
  168. ret = regmap_write(gp2ap002->map, GP2AP002_HYS,
  169. gp2ap002->hys_close);
  170. if (ret)
  171. dev_err(gp2ap002->dev,
  172. "error setting up proximity hysteresis\n");
  173. ev = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, GP2AP002_PROX_CHANNEL,
  174. IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING);
  175. }
  176. iio_push_event(indio_dev, ev, iio_get_time_ns(indio_dev));
  177. /*
  178. * After changing hysteresis, we need to wait for one detection
  179. * cycle to see if anything changed, or we will just trigger the
  180. * previous interrupt again. A detection cycle depends on the CYCLE
  181. * register, we are hard-coding ~8 ms in probe() so wait some more
  182. * than this, 20-30 ms.
  183. */
  184. usleep_range(20000, 30000);
  185. err_retrig:
  186. ret = regmap_write(gp2ap002->map, GP2AP002_CON,
  187. GP2AP002_CON_OCON_ENABLE);
  188. if (ret)
  189. dev_err(gp2ap002->dev, "error setting up VOUT control\n");
  190. return IRQ_HANDLED;
  191. }
  192. /*
  193. * This array maps current and lux.
  194. *
  195. * Ambient light sensing range is 3 to 55000 lux.
  196. *
  197. * This mapping is based on the following formula.
  198. * illuminance = 10 ^ (current[mA] / 10)
  199. *
  200. * When the ADC measures 0, return 0 lux.
  201. */
  202. static const u16 gp2ap002_illuminance_table[] = {
  203. 0, 1, 1, 2, 2, 3, 4, 5, 6, 8, 10, 12, 16, 20, 25, 32, 40, 50, 63, 79,
  204. 100, 126, 158, 200, 251, 316, 398, 501, 631, 794, 1000, 1259, 1585,
  205. 1995, 2512, 3162, 3981, 5012, 6310, 7943, 10000, 12589, 15849, 19953,
  206. 25119, 31623, 39811, 50119,
  207. };
  208. static int gp2ap002_get_lux(struct gp2ap002 *gp2ap002)
  209. {
  210. int ret, res;
  211. u16 lux;
  212. ret = iio_read_channel_processed(gp2ap002->alsout, &res);
  213. if (ret < 0)
  214. return ret;
  215. dev_dbg(gp2ap002->dev, "read %d mA from ADC\n", res);
  216. /* ensure we don't under/overflow */
  217. res = clamp(res, 0, (int)ARRAY_SIZE(gp2ap002_illuminance_table) - 1);
  218. lux = gp2ap002_illuminance_table[res];
  219. return (int)lux;
  220. }
  221. static int gp2ap002_read_raw(struct iio_dev *indio_dev,
  222. struct iio_chan_spec const *chan,
  223. int *val, int *val2, long mask)
  224. {
  225. struct gp2ap002 *gp2ap002 = iio_priv(indio_dev);
  226. int ret;
  227. pm_runtime_get_sync(gp2ap002->dev);
  228. switch (mask) {
  229. case IIO_CHAN_INFO_RAW:
  230. switch (chan->type) {
  231. case IIO_LIGHT:
  232. ret = gp2ap002_get_lux(gp2ap002);
  233. if (ret < 0)
  234. return ret;
  235. *val = ret;
  236. ret = IIO_VAL_INT;
  237. goto out;
  238. default:
  239. ret = -EINVAL;
  240. goto out;
  241. }
  242. default:
  243. ret = -EINVAL;
  244. }
  245. out:
  246. pm_runtime_mark_last_busy(gp2ap002->dev);
  247. pm_runtime_put_autosuspend(gp2ap002->dev);
  248. return ret;
  249. }
  250. static int gp2ap002_init(struct gp2ap002 *gp2ap002)
  251. {
  252. int ret;
  253. /* Set up the IR LED resistance */
  254. ret = regmap_write(gp2ap002->map, GP2AP002_GAIN,
  255. GP2AP002_GAIN_LED_NORMAL);
  256. if (ret) {
  257. dev_err(gp2ap002->dev, "error setting up LED gain\n");
  258. return ret;
  259. }
  260. ret = regmap_write(gp2ap002->map, GP2AP002_HYS, gp2ap002->hys_far);
  261. if (ret) {
  262. dev_err(gp2ap002->dev,
  263. "error setting up proximity hysteresis\n");
  264. return ret;
  265. }
  266. /* Disable internal frequency hopping */
  267. ret = regmap_write(gp2ap002->map, GP2AP002_CYCLE,
  268. GP2AP002_CYCLE_OSC_INEFFECTIVE);
  269. if (ret) {
  270. dev_err(gp2ap002->dev,
  271. "error setting up internal frequency hopping\n");
  272. return ret;
  273. }
  274. /* Enable chip and IRQ, disable analog sleep */
  275. ret = regmap_write(gp2ap002->map, GP2AP002_OPMOD,
  276. GP2AP002_OPMOD_SSD_OPERATING |
  277. GP2AP002_OPMOD_VCON_IRQ);
  278. if (ret) {
  279. dev_err(gp2ap002->dev, "error setting up operation mode\n");
  280. return ret;
  281. }
  282. /* Interrupt on VOUT enabled */
  283. ret = regmap_write(gp2ap002->map, GP2AP002_CON,
  284. GP2AP002_CON_OCON_ENABLE);
  285. if (ret)
  286. dev_err(gp2ap002->dev, "error setting up VOUT control\n");
  287. return ret;
  288. }
  289. static int gp2ap002_read_event_config(struct iio_dev *indio_dev,
  290. const struct iio_chan_spec *chan,
  291. enum iio_event_type type,
  292. enum iio_event_direction dir)
  293. {
  294. struct gp2ap002 *gp2ap002 = iio_priv(indio_dev);
  295. /*
  296. * We just keep track of this internally, as it is not possible to
  297. * query the hardware.
  298. */
  299. return gp2ap002->enabled;
  300. }
  301. static int gp2ap002_write_event_config(struct iio_dev *indio_dev,
  302. const struct iio_chan_spec *chan,
  303. enum iio_event_type type,
  304. enum iio_event_direction dir,
  305. int state)
  306. {
  307. struct gp2ap002 *gp2ap002 = iio_priv(indio_dev);
  308. if (state) {
  309. /*
  310. * This will bring the regulators up (unless they are on
  311. * already) and reintialize the sensor by using runtime_pm
  312. * callbacks.
  313. */
  314. pm_runtime_get_sync(gp2ap002->dev);
  315. gp2ap002->enabled = true;
  316. } else {
  317. pm_runtime_mark_last_busy(gp2ap002->dev);
  318. pm_runtime_put_autosuspend(gp2ap002->dev);
  319. gp2ap002->enabled = false;
  320. }
  321. return 0;
  322. }
  323. static const struct iio_info gp2ap002_info = {
  324. .read_raw = gp2ap002_read_raw,
  325. .read_event_config = gp2ap002_read_event_config,
  326. .write_event_config = gp2ap002_write_event_config,
  327. };
  328. static const struct iio_event_spec gp2ap002_events[] = {
  329. {
  330. .type = IIO_EV_TYPE_THRESH,
  331. .dir = IIO_EV_DIR_EITHER,
  332. .mask_separate = BIT(IIO_EV_INFO_ENABLE),
  333. },
  334. };
  335. static const struct iio_chan_spec gp2ap002_channels[] = {
  336. {
  337. .type = IIO_PROXIMITY,
  338. .event_spec = gp2ap002_events,
  339. .num_event_specs = ARRAY_SIZE(gp2ap002_events),
  340. },
  341. {
  342. .type = IIO_LIGHT,
  343. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  344. .channel = GP2AP002_ALS_CHANNEL,
  345. },
  346. };
  347. /*
  348. * We need a special regmap because this hardware expects to
  349. * write single bytes to registers but read a 16bit word on some
  350. * variants and discard the lower 8 bits so combine
  351. * i2c_smbus_read_word_data() with i2c_smbus_write_byte_data()
  352. * selectively like this.
  353. */
  354. static int gp2ap002_regmap_i2c_read(void *context, unsigned int reg,
  355. unsigned int *val)
  356. {
  357. struct device *dev = context;
  358. struct i2c_client *i2c = to_i2c_client(dev);
  359. int ret;
  360. ret = i2c_smbus_read_word_data(i2c, reg);
  361. if (ret < 0)
  362. return ret;
  363. *val = (ret >> 8) & 0xFF;
  364. return 0;
  365. }
  366. static int gp2ap002_regmap_i2c_write(void *context, unsigned int reg,
  367. unsigned int val)
  368. {
  369. struct device *dev = context;
  370. struct i2c_client *i2c = to_i2c_client(dev);
  371. return i2c_smbus_write_byte_data(i2c, reg, val);
  372. }
  373. static const struct regmap_bus gp2ap002_regmap_bus = {
  374. .reg_read = gp2ap002_regmap_i2c_read,
  375. .reg_write = gp2ap002_regmap_i2c_write,
  376. };
  377. static int gp2ap002_probe(struct i2c_client *client)
  378. {
  379. struct gp2ap002 *gp2ap002;
  380. struct iio_dev *indio_dev;
  381. struct device *dev = &client->dev;
  382. enum iio_chan_type ch_type;
  383. static const struct regmap_config config = {
  384. .reg_bits = 8,
  385. .val_bits = 8,
  386. .max_register = GP2AP002_CON,
  387. };
  388. struct regmap *regmap;
  389. int num_chan;
  390. const char *compat;
  391. u8 val;
  392. int ret;
  393. indio_dev = devm_iio_device_alloc(dev, sizeof(*gp2ap002));
  394. if (!indio_dev)
  395. return -ENOMEM;
  396. i2c_set_clientdata(client, indio_dev);
  397. gp2ap002 = iio_priv(indio_dev);
  398. gp2ap002->dev = dev;
  399. /*
  400. * Check the device compatible like this makes it possible to use
  401. * ACPI PRP0001 for registering the sensor using device tree
  402. * properties.
  403. */
  404. ret = device_property_read_string(dev, "compatible", &compat);
  405. if (ret) {
  406. dev_err(dev, "cannot check compatible\n");
  407. return ret;
  408. }
  409. gp2ap002->is_gp2ap002s00f = !strcmp(compat, "sharp,gp2ap002s00f");
  410. regmap = devm_regmap_init(dev, &gp2ap002_regmap_bus, dev, &config);
  411. if (IS_ERR(regmap)) {
  412. dev_err(dev, "Failed to register i2c regmap %ld\n", PTR_ERR(regmap));
  413. return PTR_ERR(regmap);
  414. }
  415. gp2ap002->map = regmap;
  416. /*
  417. * The hysteresis settings are coded into the device tree as values
  418. * to be written into the hysteresis register. The datasheet defines
  419. * modes "A", "B1" and "B2" with fixed values to be use but vendor
  420. * code trees for actual devices are tweaking these values and refer to
  421. * modes named things like "B1.5". To be able to support any devices,
  422. * we allow passing an arbitrary hysteresis setting for "near" and
  423. * "far".
  424. */
  425. /* Check the device tree for the IR LED hysteresis */
  426. ret = device_property_read_u8(dev, "sharp,proximity-far-hysteresis",
  427. &val);
  428. if (ret) {
  429. dev_err(dev, "failed to obtain proximity far setting\n");
  430. return ret;
  431. }
  432. dev_dbg(dev, "proximity far setting %02x\n", val);
  433. gp2ap002->hys_far = val;
  434. ret = device_property_read_u8(dev, "sharp,proximity-close-hysteresis",
  435. &val);
  436. if (ret) {
  437. dev_err(dev, "failed to obtain proximity close setting\n");
  438. return ret;
  439. }
  440. dev_dbg(dev, "proximity close setting %02x\n", val);
  441. gp2ap002->hys_close = val;
  442. /* The GP2AP002A00F has a light sensor too */
  443. if (!gp2ap002->is_gp2ap002s00f) {
  444. gp2ap002->alsout = devm_iio_channel_get(dev, "alsout");
  445. if (IS_ERR(gp2ap002->alsout)) {
  446. ret = PTR_ERR(gp2ap002->alsout);
  447. ret = (ret == -ENODEV) ? -EPROBE_DEFER : ret;
  448. return dev_err_probe(dev, ret, "failed to get ALSOUT ADC channel\n");
  449. }
  450. ret = iio_get_channel_type(gp2ap002->alsout, &ch_type);
  451. if (ret < 0)
  452. return ret;
  453. if (ch_type != IIO_CURRENT) {
  454. dev_err(dev,
  455. "wrong type of IIO channel specified for ALSOUT\n");
  456. return -EINVAL;
  457. }
  458. }
  459. gp2ap002->vdd = devm_regulator_get(dev, "vdd");
  460. if (IS_ERR(gp2ap002->vdd))
  461. return dev_err_probe(dev, PTR_ERR(gp2ap002->vdd),
  462. "failed to get VDD regulator\n");
  463. gp2ap002->vio = devm_regulator_get(dev, "vio");
  464. if (IS_ERR(gp2ap002->vio))
  465. return dev_err_probe(dev, PTR_ERR(gp2ap002->vio),
  466. "failed to get VIO regulator\n");
  467. /* Operating voltage 2.4V .. 3.6V according to datasheet */
  468. ret = regulator_set_voltage(gp2ap002->vdd, 2400000, 3600000);
  469. if (ret) {
  470. dev_err(dev, "failed to sett VDD voltage\n");
  471. return ret;
  472. }
  473. /* VIO should be between 1.65V and VDD */
  474. ret = regulator_get_voltage(gp2ap002->vdd);
  475. if (ret < 0) {
  476. dev_err(dev, "failed to get VDD voltage\n");
  477. return ret;
  478. }
  479. ret = regulator_set_voltage(gp2ap002->vio, 1650000, ret);
  480. if (ret) {
  481. dev_err(dev, "failed to set VIO voltage\n");
  482. return ret;
  483. }
  484. ret = regulator_enable(gp2ap002->vdd);
  485. if (ret) {
  486. dev_err(dev, "failed to enable VDD regulator\n");
  487. return ret;
  488. }
  489. ret = regulator_enable(gp2ap002->vio);
  490. if (ret) {
  491. dev_err(dev, "failed to enable VIO regulator\n");
  492. goto out_disable_vdd;
  493. }
  494. msleep(20);
  495. /*
  496. * Initialize the device and signal to runtime PM that now we are
  497. * definitely up and using power.
  498. */
  499. ret = gp2ap002_init(gp2ap002);
  500. if (ret) {
  501. dev_err(dev, "initialization failed\n");
  502. goto out_disable_vio;
  503. }
  504. pm_runtime_get_noresume(dev);
  505. pm_runtime_set_active(dev);
  506. pm_runtime_enable(dev);
  507. gp2ap002->enabled = false;
  508. ret = devm_request_threaded_irq(dev, client->irq, NULL,
  509. gp2ap002_prox_irq, IRQF_ONESHOT,
  510. "gp2ap002", indio_dev);
  511. if (ret) {
  512. dev_err(dev, "unable to request IRQ\n");
  513. goto out_put_pm;
  514. }
  515. gp2ap002->irq = client->irq;
  516. /*
  517. * As the device takes 20 ms + regulator delay to come up with a fresh
  518. * measurement after power-on, do not shut it down unnecessarily.
  519. * Set autosuspend to a one second.
  520. */
  521. pm_runtime_set_autosuspend_delay(dev, 1000);
  522. pm_runtime_use_autosuspend(dev);
  523. pm_runtime_put(dev);
  524. indio_dev->info = &gp2ap002_info;
  525. indio_dev->name = "gp2ap002";
  526. indio_dev->channels = gp2ap002_channels;
  527. /* Skip light channel for the proximity-only sensor */
  528. num_chan = ARRAY_SIZE(gp2ap002_channels);
  529. if (gp2ap002->is_gp2ap002s00f)
  530. num_chan--;
  531. indio_dev->num_channels = num_chan;
  532. indio_dev->modes = INDIO_DIRECT_MODE;
  533. ret = iio_device_register(indio_dev);
  534. if (ret)
  535. goto out_disable_pm;
  536. dev_dbg(dev, "Sharp GP2AP002 probed successfully\n");
  537. return 0;
  538. out_put_pm:
  539. pm_runtime_put_noidle(dev);
  540. out_disable_pm:
  541. pm_runtime_disable(dev);
  542. out_disable_vio:
  543. regulator_disable(gp2ap002->vio);
  544. out_disable_vdd:
  545. regulator_disable(gp2ap002->vdd);
  546. return ret;
  547. }
  548. static void gp2ap002_remove(struct i2c_client *client)
  549. {
  550. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  551. struct gp2ap002 *gp2ap002 = iio_priv(indio_dev);
  552. struct device *dev = &client->dev;
  553. pm_runtime_get_sync(dev);
  554. pm_runtime_put_noidle(dev);
  555. pm_runtime_disable(dev);
  556. iio_device_unregister(indio_dev);
  557. regulator_disable(gp2ap002->vio);
  558. regulator_disable(gp2ap002->vdd);
  559. }
  560. static int gp2ap002_runtime_suspend(struct device *dev)
  561. {
  562. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  563. struct gp2ap002 *gp2ap002 = iio_priv(indio_dev);
  564. int ret;
  565. /* Deactivate the IRQ */
  566. disable_irq(gp2ap002->irq);
  567. /* Disable chip and IRQ, everything off */
  568. ret = regmap_write(gp2ap002->map, GP2AP002_OPMOD, 0x00);
  569. if (ret) {
  570. dev_err(gp2ap002->dev, "error setting up operation mode\n");
  571. return ret;
  572. }
  573. /*
  574. * As these regulators may be shared, at least we are now in
  575. * sleep even if the regulators aren't really turned off.
  576. */
  577. regulator_disable(gp2ap002->vio);
  578. regulator_disable(gp2ap002->vdd);
  579. return 0;
  580. }
  581. static int gp2ap002_runtime_resume(struct device *dev)
  582. {
  583. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  584. struct gp2ap002 *gp2ap002 = iio_priv(indio_dev);
  585. int ret;
  586. ret = regulator_enable(gp2ap002->vdd);
  587. if (ret) {
  588. dev_err(dev, "failed to enable VDD regulator in resume path\n");
  589. return ret;
  590. }
  591. ret = regulator_enable(gp2ap002->vio);
  592. if (ret) {
  593. dev_err(dev, "failed to enable VIO regulator in resume path\n");
  594. return ret;
  595. }
  596. msleep(20);
  597. ret = gp2ap002_init(gp2ap002);
  598. if (ret) {
  599. dev_err(dev, "re-initialization failed\n");
  600. return ret;
  601. }
  602. /* Re-activate the IRQ */
  603. enable_irq(gp2ap002->irq);
  604. return 0;
  605. }
  606. static DEFINE_RUNTIME_DEV_PM_OPS(gp2ap002_dev_pm_ops, gp2ap002_runtime_suspend,
  607. gp2ap002_runtime_resume, NULL);
  608. static const struct i2c_device_id gp2ap002_id_table[] = {
  609. { "gp2ap002" },
  610. { }
  611. };
  612. MODULE_DEVICE_TABLE(i2c, gp2ap002_id_table);
  613. static const struct of_device_id gp2ap002_of_match[] = {
  614. { .compatible = "sharp,gp2ap002a00f" },
  615. { .compatible = "sharp,gp2ap002s00f" },
  616. { },
  617. };
  618. MODULE_DEVICE_TABLE(of, gp2ap002_of_match);
  619. static struct i2c_driver gp2ap002_driver = {
  620. .driver = {
  621. .name = "gp2ap002",
  622. .of_match_table = gp2ap002_of_match,
  623. .pm = pm_ptr(&gp2ap002_dev_pm_ops),
  624. },
  625. .probe = gp2ap002_probe,
  626. .remove = gp2ap002_remove,
  627. .id_table = gp2ap002_id_table,
  628. };
  629. module_i2c_driver(gp2ap002_driver);
  630. MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
  631. MODULE_DESCRIPTION("GP2AP002 ambient light and proximity sensor driver");
  632. MODULE_LICENSE("GPL v2");