stk3310.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Sensortek STK3310/STK3311 Ambient Light and Proximity Sensor
  4. *
  5. * Copyright (c) 2015, Intel Corporation.
  6. *
  7. * IIO driver for STK3310/STK3311. 7-bit I2C address: 0x48.
  8. */
  9. #include <linux/acpi.h>
  10. #include <linux/i2c.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/regmap.h>
  15. #include <linux/iio/events.h>
  16. #include <linux/iio/iio.h>
  17. #include <linux/iio/sysfs.h>
  18. #define STK3310_REG_STATE 0x00
  19. #define STK3310_REG_PSCTRL 0x01
  20. #define STK3310_REG_ALSCTRL 0x02
  21. #define STK3310_REG_INT 0x04
  22. #define STK3310_REG_THDH_PS 0x06
  23. #define STK3310_REG_THDL_PS 0x08
  24. #define STK3310_REG_FLAG 0x10
  25. #define STK3310_REG_PS_DATA_MSB 0x11
  26. #define STK3310_REG_PS_DATA_LSB 0x12
  27. #define STK3310_REG_ALS_DATA_MSB 0x13
  28. #define STK3310_REG_ALS_DATA_LSB 0x14
  29. #define STK3310_REG_ID 0x3E
  30. #define STK3310_MAX_REG 0x80
  31. #define STK3310_STATE_EN_PS BIT(0)
  32. #define STK3310_STATE_EN_ALS BIT(1)
  33. #define STK3310_STATE_STANDBY 0x00
  34. #define STK3310_CHIP_ID_VAL 0x13
  35. #define STK3311_CHIP_ID_VAL 0x1D
  36. #define STK3311X_CHIP_ID_VAL 0x12
  37. #define STK3335_CHIP_ID_VAL 0x51
  38. #define STK3310_PSINT_EN 0x01
  39. #define STK3310_PS_MAX_VAL 0xFFFF
  40. #define STK3310_DRIVER_NAME "stk3310"
  41. #define STK3310_REGMAP_NAME "stk3310_regmap"
  42. #define STK3310_EVENT "stk3310_event"
  43. #define STK3310_SCALE_AVAILABLE "6.4 1.6 0.4 0.1"
  44. #define STK3310_IT_AVAILABLE \
  45. "0.000185 0.000370 0.000741 0.001480 0.002960 0.005920 0.011840 " \
  46. "0.023680 0.047360 0.094720 0.189440 0.378880 0.757760 1.515520 " \
  47. "3.031040 6.062080"
  48. #define STK3310_REGFIELD(name) \
  49. do { \
  50. data->reg_##name = \
  51. devm_regmap_field_alloc(&client->dev, regmap, \
  52. stk3310_reg_field_##name); \
  53. if (IS_ERR(data->reg_##name)) { \
  54. dev_err(&client->dev, "reg field alloc failed.\n"); \
  55. return PTR_ERR(data->reg_##name); \
  56. } \
  57. } while (0)
  58. static const struct reg_field stk3310_reg_field_state =
  59. REG_FIELD(STK3310_REG_STATE, 0, 2);
  60. static const struct reg_field stk3310_reg_field_als_gain =
  61. REG_FIELD(STK3310_REG_ALSCTRL, 4, 5);
  62. static const struct reg_field stk3310_reg_field_ps_gain =
  63. REG_FIELD(STK3310_REG_PSCTRL, 4, 5);
  64. static const struct reg_field stk3310_reg_field_als_it =
  65. REG_FIELD(STK3310_REG_ALSCTRL, 0, 3);
  66. static const struct reg_field stk3310_reg_field_ps_it =
  67. REG_FIELD(STK3310_REG_PSCTRL, 0, 3);
  68. static const struct reg_field stk3310_reg_field_int_ps =
  69. REG_FIELD(STK3310_REG_INT, 0, 2);
  70. static const struct reg_field stk3310_reg_field_flag_psint =
  71. REG_FIELD(STK3310_REG_FLAG, 4, 4);
  72. static const struct reg_field stk3310_reg_field_flag_nf =
  73. REG_FIELD(STK3310_REG_FLAG, 0, 0);
  74. /* Estimate maximum proximity values with regard to measurement scale. */
  75. static const int stk3310_ps_max[4] = {
  76. STK3310_PS_MAX_VAL / 640,
  77. STK3310_PS_MAX_VAL / 160,
  78. STK3310_PS_MAX_VAL / 40,
  79. STK3310_PS_MAX_VAL / 10
  80. };
  81. static const int stk3310_scale_table[][2] = {
  82. {6, 400000}, {1, 600000}, {0, 400000}, {0, 100000}
  83. };
  84. /* Integration time in seconds, microseconds */
  85. static const int stk3310_it_table[][2] = {
  86. {0, 185}, {0, 370}, {0, 741}, {0, 1480},
  87. {0, 2960}, {0, 5920}, {0, 11840}, {0, 23680},
  88. {0, 47360}, {0, 94720}, {0, 189440}, {0, 378880},
  89. {0, 757760}, {1, 515520}, {3, 31040}, {6, 62080},
  90. };
  91. struct stk3310_data {
  92. struct i2c_client *client;
  93. struct mutex lock;
  94. bool als_enabled;
  95. bool ps_enabled;
  96. uint32_t ps_near_level;
  97. u64 timestamp;
  98. struct regmap *regmap;
  99. struct regmap_field *reg_state;
  100. struct regmap_field *reg_als_gain;
  101. struct regmap_field *reg_ps_gain;
  102. struct regmap_field *reg_als_it;
  103. struct regmap_field *reg_ps_it;
  104. struct regmap_field *reg_int_ps;
  105. struct regmap_field *reg_flag_psint;
  106. struct regmap_field *reg_flag_nf;
  107. };
  108. static const struct iio_event_spec stk3310_events[] = {
  109. /* Proximity event */
  110. {
  111. .type = IIO_EV_TYPE_THRESH,
  112. .dir = IIO_EV_DIR_RISING,
  113. .mask_separate = BIT(IIO_EV_INFO_VALUE) |
  114. BIT(IIO_EV_INFO_ENABLE),
  115. },
  116. /* Out-of-proximity event */
  117. {
  118. .type = IIO_EV_TYPE_THRESH,
  119. .dir = IIO_EV_DIR_FALLING,
  120. .mask_separate = BIT(IIO_EV_INFO_VALUE) |
  121. BIT(IIO_EV_INFO_ENABLE),
  122. },
  123. };
  124. static ssize_t stk3310_read_near_level(struct iio_dev *indio_dev,
  125. uintptr_t priv,
  126. const struct iio_chan_spec *chan,
  127. char *buf)
  128. {
  129. struct stk3310_data *data = iio_priv(indio_dev);
  130. return sprintf(buf, "%u\n", data->ps_near_level);
  131. }
  132. static const struct iio_chan_spec_ext_info stk3310_ext_info[] = {
  133. {
  134. .name = "nearlevel",
  135. .shared = IIO_SEPARATE,
  136. .read = stk3310_read_near_level,
  137. },
  138. { /* sentinel */ }
  139. };
  140. static const struct iio_chan_spec stk3310_channels[] = {
  141. {
  142. .type = IIO_LIGHT,
  143. .info_mask_separate =
  144. BIT(IIO_CHAN_INFO_RAW) |
  145. BIT(IIO_CHAN_INFO_SCALE) |
  146. BIT(IIO_CHAN_INFO_INT_TIME),
  147. },
  148. {
  149. .type = IIO_PROXIMITY,
  150. .info_mask_separate =
  151. BIT(IIO_CHAN_INFO_RAW) |
  152. BIT(IIO_CHAN_INFO_SCALE) |
  153. BIT(IIO_CHAN_INFO_INT_TIME),
  154. .event_spec = stk3310_events,
  155. .num_event_specs = ARRAY_SIZE(stk3310_events),
  156. .ext_info = stk3310_ext_info,
  157. }
  158. };
  159. static IIO_CONST_ATTR(in_illuminance_scale_available, STK3310_SCALE_AVAILABLE);
  160. static IIO_CONST_ATTR(in_proximity_scale_available, STK3310_SCALE_AVAILABLE);
  161. static IIO_CONST_ATTR(in_illuminance_integration_time_available,
  162. STK3310_IT_AVAILABLE);
  163. static IIO_CONST_ATTR(in_proximity_integration_time_available,
  164. STK3310_IT_AVAILABLE);
  165. static struct attribute *stk3310_attributes[] = {
  166. &iio_const_attr_in_illuminance_scale_available.dev_attr.attr,
  167. &iio_const_attr_in_proximity_scale_available.dev_attr.attr,
  168. &iio_const_attr_in_illuminance_integration_time_available.dev_attr.attr,
  169. &iio_const_attr_in_proximity_integration_time_available.dev_attr.attr,
  170. NULL,
  171. };
  172. static const struct attribute_group stk3310_attribute_group = {
  173. .attrs = stk3310_attributes
  174. };
  175. static int stk3310_get_index(const int table[][2], int table_size,
  176. int val, int val2)
  177. {
  178. int i;
  179. for (i = 0; i < table_size; i++) {
  180. if (val == table[i][0] && val2 == table[i][1])
  181. return i;
  182. }
  183. return -EINVAL;
  184. }
  185. static int stk3310_read_event(struct iio_dev *indio_dev,
  186. const struct iio_chan_spec *chan,
  187. enum iio_event_type type,
  188. enum iio_event_direction dir,
  189. enum iio_event_info info,
  190. int *val, int *val2)
  191. {
  192. u8 reg;
  193. __be16 buf;
  194. int ret;
  195. struct stk3310_data *data = iio_priv(indio_dev);
  196. if (info != IIO_EV_INFO_VALUE)
  197. return -EINVAL;
  198. /* Only proximity interrupts are implemented at the moment. */
  199. if (dir == IIO_EV_DIR_RISING)
  200. reg = STK3310_REG_THDH_PS;
  201. else if (dir == IIO_EV_DIR_FALLING)
  202. reg = STK3310_REG_THDL_PS;
  203. else
  204. return -EINVAL;
  205. mutex_lock(&data->lock);
  206. ret = regmap_bulk_read(data->regmap, reg, &buf, 2);
  207. mutex_unlock(&data->lock);
  208. if (ret < 0) {
  209. dev_err(&data->client->dev, "register read failed\n");
  210. return ret;
  211. }
  212. *val = be16_to_cpu(buf);
  213. return IIO_VAL_INT;
  214. }
  215. static int stk3310_write_event(struct iio_dev *indio_dev,
  216. const struct iio_chan_spec *chan,
  217. enum iio_event_type type,
  218. enum iio_event_direction dir,
  219. enum iio_event_info info,
  220. int val, int val2)
  221. {
  222. u8 reg;
  223. __be16 buf;
  224. int ret;
  225. unsigned int index;
  226. struct stk3310_data *data = iio_priv(indio_dev);
  227. struct i2c_client *client = data->client;
  228. ret = regmap_field_read(data->reg_ps_gain, &index);
  229. if (ret < 0)
  230. return ret;
  231. if (val < 0 || val > stk3310_ps_max[index])
  232. return -EINVAL;
  233. if (dir == IIO_EV_DIR_RISING)
  234. reg = STK3310_REG_THDH_PS;
  235. else if (dir == IIO_EV_DIR_FALLING)
  236. reg = STK3310_REG_THDL_PS;
  237. else
  238. return -EINVAL;
  239. buf = cpu_to_be16(val);
  240. ret = regmap_bulk_write(data->regmap, reg, &buf, 2);
  241. if (ret < 0)
  242. dev_err(&client->dev, "failed to set PS threshold!\n");
  243. return ret;
  244. }
  245. static int stk3310_read_event_config(struct iio_dev *indio_dev,
  246. const struct iio_chan_spec *chan,
  247. enum iio_event_type type,
  248. enum iio_event_direction dir)
  249. {
  250. unsigned int event_val;
  251. int ret;
  252. struct stk3310_data *data = iio_priv(indio_dev);
  253. ret = regmap_field_read(data->reg_int_ps, &event_val);
  254. if (ret < 0)
  255. return ret;
  256. return event_val;
  257. }
  258. static int stk3310_write_event_config(struct iio_dev *indio_dev,
  259. const struct iio_chan_spec *chan,
  260. enum iio_event_type type,
  261. enum iio_event_direction dir,
  262. int state)
  263. {
  264. int ret;
  265. struct stk3310_data *data = iio_priv(indio_dev);
  266. struct i2c_client *client = data->client;
  267. if (state < 0 || state > 7)
  268. return -EINVAL;
  269. /* Set INT_PS value */
  270. mutex_lock(&data->lock);
  271. ret = regmap_field_write(data->reg_int_ps, state);
  272. if (ret < 0)
  273. dev_err(&client->dev, "failed to set interrupt mode\n");
  274. mutex_unlock(&data->lock);
  275. return ret;
  276. }
  277. static int stk3310_read_raw(struct iio_dev *indio_dev,
  278. struct iio_chan_spec const *chan,
  279. int *val, int *val2, long mask)
  280. {
  281. u8 reg;
  282. __be16 buf;
  283. int ret;
  284. unsigned int index;
  285. struct stk3310_data *data = iio_priv(indio_dev);
  286. struct i2c_client *client = data->client;
  287. if (chan->type != IIO_LIGHT && chan->type != IIO_PROXIMITY)
  288. return -EINVAL;
  289. switch (mask) {
  290. case IIO_CHAN_INFO_RAW:
  291. if (chan->type == IIO_LIGHT)
  292. reg = STK3310_REG_ALS_DATA_MSB;
  293. else
  294. reg = STK3310_REG_PS_DATA_MSB;
  295. mutex_lock(&data->lock);
  296. ret = regmap_bulk_read(data->regmap, reg, &buf, 2);
  297. if (ret < 0) {
  298. dev_err(&client->dev, "register read failed\n");
  299. mutex_unlock(&data->lock);
  300. return ret;
  301. }
  302. *val = be16_to_cpu(buf);
  303. mutex_unlock(&data->lock);
  304. return IIO_VAL_INT;
  305. case IIO_CHAN_INFO_INT_TIME:
  306. if (chan->type == IIO_LIGHT)
  307. ret = regmap_field_read(data->reg_als_it, &index);
  308. else
  309. ret = regmap_field_read(data->reg_ps_it, &index);
  310. if (ret < 0)
  311. return ret;
  312. *val = stk3310_it_table[index][0];
  313. *val2 = stk3310_it_table[index][1];
  314. return IIO_VAL_INT_PLUS_MICRO;
  315. case IIO_CHAN_INFO_SCALE:
  316. if (chan->type == IIO_LIGHT)
  317. ret = regmap_field_read(data->reg_als_gain, &index);
  318. else
  319. ret = regmap_field_read(data->reg_ps_gain, &index);
  320. if (ret < 0)
  321. return ret;
  322. *val = stk3310_scale_table[index][0];
  323. *val2 = stk3310_scale_table[index][1];
  324. return IIO_VAL_INT_PLUS_MICRO;
  325. }
  326. return -EINVAL;
  327. }
  328. static int stk3310_write_raw(struct iio_dev *indio_dev,
  329. struct iio_chan_spec const *chan,
  330. int val, int val2, long mask)
  331. {
  332. int ret;
  333. int index;
  334. struct stk3310_data *data = iio_priv(indio_dev);
  335. if (chan->type != IIO_LIGHT && chan->type != IIO_PROXIMITY)
  336. return -EINVAL;
  337. switch (mask) {
  338. case IIO_CHAN_INFO_INT_TIME:
  339. index = stk3310_get_index(stk3310_it_table,
  340. ARRAY_SIZE(stk3310_it_table),
  341. val, val2);
  342. if (index < 0)
  343. return -EINVAL;
  344. mutex_lock(&data->lock);
  345. if (chan->type == IIO_LIGHT)
  346. ret = regmap_field_write(data->reg_als_it, index);
  347. else
  348. ret = regmap_field_write(data->reg_ps_it, index);
  349. if (ret < 0)
  350. dev_err(&data->client->dev,
  351. "sensor configuration failed\n");
  352. mutex_unlock(&data->lock);
  353. return ret;
  354. case IIO_CHAN_INFO_SCALE:
  355. index = stk3310_get_index(stk3310_scale_table,
  356. ARRAY_SIZE(stk3310_scale_table),
  357. val, val2);
  358. if (index < 0)
  359. return -EINVAL;
  360. mutex_lock(&data->lock);
  361. if (chan->type == IIO_LIGHT)
  362. ret = regmap_field_write(data->reg_als_gain, index);
  363. else
  364. ret = regmap_field_write(data->reg_ps_gain, index);
  365. if (ret < 0)
  366. dev_err(&data->client->dev,
  367. "sensor configuration failed\n");
  368. mutex_unlock(&data->lock);
  369. return ret;
  370. }
  371. return -EINVAL;
  372. }
  373. static const struct iio_info stk3310_info = {
  374. .read_raw = stk3310_read_raw,
  375. .write_raw = stk3310_write_raw,
  376. .attrs = &stk3310_attribute_group,
  377. .read_event_value = stk3310_read_event,
  378. .write_event_value = stk3310_write_event,
  379. .read_event_config = stk3310_read_event_config,
  380. .write_event_config = stk3310_write_event_config,
  381. };
  382. static int stk3310_set_state(struct stk3310_data *data, u8 state)
  383. {
  384. int ret;
  385. struct i2c_client *client = data->client;
  386. /* 3-bit state; 0b100 is not supported. */
  387. if (state > 7 || state == 4)
  388. return -EINVAL;
  389. mutex_lock(&data->lock);
  390. ret = regmap_field_write(data->reg_state, state);
  391. if (ret < 0) {
  392. dev_err(&client->dev, "failed to change sensor state\n");
  393. } else if (state != STK3310_STATE_STANDBY) {
  394. /* Don't reset the 'enabled' flags if we're going in standby */
  395. data->ps_enabled = !!(state & STK3310_STATE_EN_PS);
  396. data->als_enabled = !!(state & STK3310_STATE_EN_ALS);
  397. }
  398. mutex_unlock(&data->lock);
  399. return ret;
  400. }
  401. static int stk3310_init(struct iio_dev *indio_dev)
  402. {
  403. int ret;
  404. int chipid;
  405. u8 state;
  406. struct stk3310_data *data = iio_priv(indio_dev);
  407. struct i2c_client *client = data->client;
  408. ret = regmap_read(data->regmap, STK3310_REG_ID, &chipid);
  409. if (ret < 0)
  410. return ret;
  411. if (chipid != STK3310_CHIP_ID_VAL &&
  412. chipid != STK3311_CHIP_ID_VAL &&
  413. chipid != STK3311X_CHIP_ID_VAL &&
  414. chipid != STK3335_CHIP_ID_VAL) {
  415. dev_err(&client->dev, "invalid chip id: 0x%x\n", chipid);
  416. return -ENODEV;
  417. }
  418. state = STK3310_STATE_EN_ALS | STK3310_STATE_EN_PS;
  419. ret = stk3310_set_state(data, state);
  420. if (ret < 0) {
  421. dev_err(&client->dev, "failed to enable sensor");
  422. return ret;
  423. }
  424. /* Enable PS interrupts */
  425. ret = regmap_field_write(data->reg_int_ps, STK3310_PSINT_EN);
  426. if (ret < 0)
  427. dev_err(&client->dev, "failed to enable interrupts!\n");
  428. return ret;
  429. }
  430. static bool stk3310_is_volatile_reg(struct device *dev, unsigned int reg)
  431. {
  432. switch (reg) {
  433. case STK3310_REG_ALS_DATA_MSB:
  434. case STK3310_REG_ALS_DATA_LSB:
  435. case STK3310_REG_PS_DATA_LSB:
  436. case STK3310_REG_PS_DATA_MSB:
  437. case STK3310_REG_FLAG:
  438. return true;
  439. default:
  440. return false;
  441. }
  442. }
  443. static const struct regmap_config stk3310_regmap_config = {
  444. .name = STK3310_REGMAP_NAME,
  445. .reg_bits = 8,
  446. .val_bits = 8,
  447. .max_register = STK3310_MAX_REG,
  448. .cache_type = REGCACHE_RBTREE,
  449. .volatile_reg = stk3310_is_volatile_reg,
  450. };
  451. static int stk3310_regmap_init(struct stk3310_data *data)
  452. {
  453. struct regmap *regmap;
  454. struct i2c_client *client;
  455. client = data->client;
  456. regmap = devm_regmap_init_i2c(client, &stk3310_regmap_config);
  457. if (IS_ERR(regmap)) {
  458. dev_err(&client->dev, "regmap initialization failed.\n");
  459. return PTR_ERR(regmap);
  460. }
  461. data->regmap = regmap;
  462. STK3310_REGFIELD(state);
  463. STK3310_REGFIELD(als_gain);
  464. STK3310_REGFIELD(ps_gain);
  465. STK3310_REGFIELD(als_it);
  466. STK3310_REGFIELD(ps_it);
  467. STK3310_REGFIELD(int_ps);
  468. STK3310_REGFIELD(flag_psint);
  469. STK3310_REGFIELD(flag_nf);
  470. return 0;
  471. }
  472. static irqreturn_t stk3310_irq_handler(int irq, void *private)
  473. {
  474. struct iio_dev *indio_dev = private;
  475. struct stk3310_data *data = iio_priv(indio_dev);
  476. data->timestamp = iio_get_time_ns(indio_dev);
  477. return IRQ_WAKE_THREAD;
  478. }
  479. static irqreturn_t stk3310_irq_event_handler(int irq, void *private)
  480. {
  481. int ret;
  482. unsigned int dir;
  483. u64 event;
  484. struct iio_dev *indio_dev = private;
  485. struct stk3310_data *data = iio_priv(indio_dev);
  486. /* Read FLAG_NF to figure out what threshold has been met. */
  487. mutex_lock(&data->lock);
  488. ret = regmap_field_read(data->reg_flag_nf, &dir);
  489. if (ret < 0) {
  490. dev_err(&data->client->dev, "register read failed: %d\n", ret);
  491. goto out;
  492. }
  493. event = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, 1,
  494. IIO_EV_TYPE_THRESH,
  495. (dir ? IIO_EV_DIR_FALLING :
  496. IIO_EV_DIR_RISING));
  497. iio_push_event(indio_dev, event, data->timestamp);
  498. /* Reset the interrupt flag */
  499. ret = regmap_field_write(data->reg_flag_psint, 0);
  500. if (ret < 0)
  501. dev_err(&data->client->dev, "failed to reset interrupts\n");
  502. out:
  503. mutex_unlock(&data->lock);
  504. return IRQ_HANDLED;
  505. }
  506. static int stk3310_probe(struct i2c_client *client,
  507. const struct i2c_device_id *id)
  508. {
  509. int ret;
  510. struct iio_dev *indio_dev;
  511. struct stk3310_data *data;
  512. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  513. if (!indio_dev) {
  514. dev_err(&client->dev, "iio allocation failed!\n");
  515. return -ENOMEM;
  516. }
  517. data = iio_priv(indio_dev);
  518. data->client = client;
  519. i2c_set_clientdata(client, indio_dev);
  520. device_property_read_u32(&client->dev, "proximity-near-level",
  521. &data->ps_near_level);
  522. mutex_init(&data->lock);
  523. ret = stk3310_regmap_init(data);
  524. if (ret < 0)
  525. return ret;
  526. indio_dev->info = &stk3310_info;
  527. indio_dev->name = STK3310_DRIVER_NAME;
  528. indio_dev->modes = INDIO_DIRECT_MODE;
  529. indio_dev->channels = stk3310_channels;
  530. indio_dev->num_channels = ARRAY_SIZE(stk3310_channels);
  531. ret = stk3310_init(indio_dev);
  532. if (ret < 0)
  533. return ret;
  534. if (client->irq > 0) {
  535. ret = devm_request_threaded_irq(&client->dev, client->irq,
  536. stk3310_irq_handler,
  537. stk3310_irq_event_handler,
  538. IRQF_TRIGGER_FALLING |
  539. IRQF_ONESHOT,
  540. STK3310_EVENT, indio_dev);
  541. if (ret < 0) {
  542. dev_err(&client->dev, "request irq %d failed\n",
  543. client->irq);
  544. goto err_standby;
  545. }
  546. }
  547. ret = iio_device_register(indio_dev);
  548. if (ret < 0) {
  549. dev_err(&client->dev, "device_register failed\n");
  550. goto err_standby;
  551. }
  552. return 0;
  553. err_standby:
  554. stk3310_set_state(data, STK3310_STATE_STANDBY);
  555. return ret;
  556. }
  557. static void stk3310_remove(struct i2c_client *client)
  558. {
  559. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  560. iio_device_unregister(indio_dev);
  561. stk3310_set_state(iio_priv(indio_dev), STK3310_STATE_STANDBY);
  562. }
  563. static int stk3310_suspend(struct device *dev)
  564. {
  565. struct stk3310_data *data;
  566. data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
  567. return stk3310_set_state(data, STK3310_STATE_STANDBY);
  568. }
  569. static int stk3310_resume(struct device *dev)
  570. {
  571. u8 state = 0;
  572. struct stk3310_data *data;
  573. data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
  574. if (data->ps_enabled)
  575. state |= STK3310_STATE_EN_PS;
  576. if (data->als_enabled)
  577. state |= STK3310_STATE_EN_ALS;
  578. return stk3310_set_state(data, state);
  579. }
  580. static DEFINE_SIMPLE_DEV_PM_OPS(stk3310_pm_ops, stk3310_suspend,
  581. stk3310_resume);
  582. static const struct i2c_device_id stk3310_i2c_id[] = {
  583. {"STK3310", 0},
  584. {"STK3311", 0},
  585. {"STK3335", 0},
  586. {}
  587. };
  588. MODULE_DEVICE_TABLE(i2c, stk3310_i2c_id);
  589. static const struct acpi_device_id stk3310_acpi_id[] = {
  590. {"STK3310", 0},
  591. {"STK3311", 0},
  592. {"STK3335", 0},
  593. {}
  594. };
  595. MODULE_DEVICE_TABLE(acpi, stk3310_acpi_id);
  596. static const struct of_device_id stk3310_of_match[] = {
  597. { .compatible = "sensortek,stk3310", },
  598. { .compatible = "sensortek,stk3311", },
  599. { .compatible = "sensortek,stk3335", },
  600. {}
  601. };
  602. MODULE_DEVICE_TABLE(of, stk3310_of_match);
  603. static struct i2c_driver stk3310_driver = {
  604. .driver = {
  605. .name = "stk3310",
  606. .of_match_table = stk3310_of_match,
  607. .pm = pm_sleep_ptr(&stk3310_pm_ops),
  608. .acpi_match_table = ACPI_PTR(stk3310_acpi_id),
  609. },
  610. .probe = stk3310_probe,
  611. .remove = stk3310_remove,
  612. .id_table = stk3310_i2c_id,
  613. };
  614. module_i2c_driver(stk3310_driver);
  615. MODULE_AUTHOR("Tiberiu Breana <[email protected]>");
  616. MODULE_DESCRIPTION("STK3310 Ambient Light and Proximity Sensor driver");
  617. MODULE_LICENSE("GPL v2");