stk3310.c 18 KB

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