stk3310.c 19 KB

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