stk3310.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  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. .read_raw = stk3310_read_raw,
  358. .write_raw = stk3310_write_raw,
  359. .attrs = &stk3310_attribute_group,
  360. .read_event_value = stk3310_read_event,
  361. .write_event_value = stk3310_write_event,
  362. .read_event_config = stk3310_read_event_config,
  363. .write_event_config = stk3310_write_event_config,
  364. };
  365. static int stk3310_set_state(struct stk3310_data *data, u8 state)
  366. {
  367. int ret;
  368. struct i2c_client *client = data->client;
  369. /* 3-bit state; 0b100 is not supported. */
  370. if (state > 7 || state == 4)
  371. return -EINVAL;
  372. mutex_lock(&data->lock);
  373. ret = regmap_field_write(data->reg_state, state);
  374. if (ret < 0) {
  375. dev_err(&client->dev, "failed to change sensor state\n");
  376. } else if (state != STK3310_STATE_STANDBY) {
  377. /* Don't reset the 'enabled' flags if we're going in standby */
  378. data->ps_enabled = !!(state & STK3310_STATE_EN_PS);
  379. data->als_enabled = !!(state & STK3310_STATE_EN_ALS);
  380. }
  381. mutex_unlock(&data->lock);
  382. return ret;
  383. }
  384. static int stk3310_init(struct iio_dev *indio_dev)
  385. {
  386. int ret;
  387. int chipid;
  388. u8 state;
  389. struct stk3310_data *data = iio_priv(indio_dev);
  390. struct i2c_client *client = data->client;
  391. ret = regmap_read(data->regmap, STK3310_REG_ID, &chipid);
  392. if (ret < 0)
  393. return ret;
  394. if (chipid != STK3310_CHIP_ID_VAL &&
  395. chipid != STK3311_CHIP_ID_VAL) {
  396. dev_err(&client->dev, "invalid chip id: 0x%x\n", chipid);
  397. return -ENODEV;
  398. }
  399. state = STK3310_STATE_EN_ALS | STK3310_STATE_EN_PS;
  400. ret = stk3310_set_state(data, state);
  401. if (ret < 0) {
  402. dev_err(&client->dev, "failed to enable sensor");
  403. return ret;
  404. }
  405. /* Enable PS interrupts */
  406. ret = regmap_field_write(data->reg_int_ps, STK3310_PSINT_EN);
  407. if (ret < 0)
  408. dev_err(&client->dev, "failed to enable interrupts!\n");
  409. return ret;
  410. }
  411. static bool stk3310_is_volatile_reg(struct device *dev, unsigned int reg)
  412. {
  413. switch (reg) {
  414. case STK3310_REG_ALS_DATA_MSB:
  415. case STK3310_REG_ALS_DATA_LSB:
  416. case STK3310_REG_PS_DATA_LSB:
  417. case STK3310_REG_PS_DATA_MSB:
  418. case STK3310_REG_FLAG:
  419. return true;
  420. default:
  421. return false;
  422. }
  423. }
  424. static struct regmap_config stk3310_regmap_config = {
  425. .name = STK3310_REGMAP_NAME,
  426. .reg_bits = 8,
  427. .val_bits = 8,
  428. .max_register = STK3310_MAX_REG,
  429. .cache_type = REGCACHE_RBTREE,
  430. .volatile_reg = stk3310_is_volatile_reg,
  431. };
  432. static int stk3310_regmap_init(struct stk3310_data *data)
  433. {
  434. struct regmap *regmap;
  435. struct i2c_client *client;
  436. client = data->client;
  437. regmap = devm_regmap_init_i2c(client, &stk3310_regmap_config);
  438. if (IS_ERR(regmap)) {
  439. dev_err(&client->dev, "regmap initialization failed.\n");
  440. return PTR_ERR(regmap);
  441. }
  442. data->regmap = regmap;
  443. STK3310_REGFIELD(state);
  444. STK3310_REGFIELD(als_gain);
  445. STK3310_REGFIELD(ps_gain);
  446. STK3310_REGFIELD(als_it);
  447. STK3310_REGFIELD(ps_it);
  448. STK3310_REGFIELD(int_ps);
  449. STK3310_REGFIELD(flag_psint);
  450. STK3310_REGFIELD(flag_nf);
  451. return 0;
  452. }
  453. static irqreturn_t stk3310_irq_handler(int irq, void *private)
  454. {
  455. struct iio_dev *indio_dev = private;
  456. struct stk3310_data *data = iio_priv(indio_dev);
  457. data->timestamp = iio_get_time_ns(indio_dev);
  458. return IRQ_WAKE_THREAD;
  459. }
  460. static irqreturn_t stk3310_irq_event_handler(int irq, void *private)
  461. {
  462. int ret;
  463. unsigned int dir;
  464. u64 event;
  465. struct iio_dev *indio_dev = private;
  466. struct stk3310_data *data = iio_priv(indio_dev);
  467. /* Read FLAG_NF to figure out what threshold has been met. */
  468. mutex_lock(&data->lock);
  469. ret = regmap_field_read(data->reg_flag_nf, &dir);
  470. if (ret < 0) {
  471. dev_err(&data->client->dev, "register read failed\n");
  472. mutex_unlock(&data->lock);
  473. return ret;
  474. }
  475. event = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, 1,
  476. IIO_EV_TYPE_THRESH,
  477. (dir ? IIO_EV_DIR_FALLING :
  478. IIO_EV_DIR_RISING));
  479. iio_push_event(indio_dev, event, data->timestamp);
  480. /* Reset the interrupt flag */
  481. ret = regmap_field_write(data->reg_flag_psint, 0);
  482. if (ret < 0)
  483. dev_err(&data->client->dev, "failed to reset interrupts\n");
  484. mutex_unlock(&data->lock);
  485. return IRQ_HANDLED;
  486. }
  487. static int stk3310_probe(struct i2c_client *client,
  488. const struct i2c_device_id *id)
  489. {
  490. int ret;
  491. struct iio_dev *indio_dev;
  492. struct stk3310_data *data;
  493. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  494. if (!indio_dev) {
  495. dev_err(&client->dev, "iio allocation failed!\n");
  496. return -ENOMEM;
  497. }
  498. data = iio_priv(indio_dev);
  499. data->client = client;
  500. i2c_set_clientdata(client, indio_dev);
  501. mutex_init(&data->lock);
  502. ret = stk3310_regmap_init(data);
  503. if (ret < 0)
  504. return ret;
  505. indio_dev->dev.parent = &client->dev;
  506. indio_dev->info = &stk3310_info;
  507. indio_dev->name = STK3310_DRIVER_NAME;
  508. indio_dev->modes = INDIO_DIRECT_MODE;
  509. indio_dev->channels = stk3310_channels;
  510. indio_dev->num_channels = ARRAY_SIZE(stk3310_channels);
  511. ret = stk3310_init(indio_dev);
  512. if (ret < 0)
  513. return ret;
  514. if (client->irq > 0) {
  515. ret = devm_request_threaded_irq(&client->dev, client->irq,
  516. stk3310_irq_handler,
  517. stk3310_irq_event_handler,
  518. IRQF_TRIGGER_FALLING |
  519. IRQF_ONESHOT,
  520. STK3310_EVENT, indio_dev);
  521. if (ret < 0) {
  522. dev_err(&client->dev, "request irq %d failed\n",
  523. client->irq);
  524. goto err_standby;
  525. }
  526. }
  527. ret = iio_device_register(indio_dev);
  528. if (ret < 0) {
  529. dev_err(&client->dev, "device_register failed\n");
  530. goto err_standby;
  531. }
  532. return 0;
  533. err_standby:
  534. stk3310_set_state(data, STK3310_STATE_STANDBY);
  535. return ret;
  536. }
  537. static int stk3310_remove(struct i2c_client *client)
  538. {
  539. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  540. iio_device_unregister(indio_dev);
  541. return stk3310_set_state(iio_priv(indio_dev), STK3310_STATE_STANDBY);
  542. }
  543. #ifdef CONFIG_PM_SLEEP
  544. static int stk3310_suspend(struct device *dev)
  545. {
  546. struct stk3310_data *data;
  547. data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
  548. return stk3310_set_state(data, STK3310_STATE_STANDBY);
  549. }
  550. static int stk3310_resume(struct device *dev)
  551. {
  552. u8 state = 0;
  553. struct stk3310_data *data;
  554. data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
  555. if (data->ps_enabled)
  556. state |= STK3310_STATE_EN_PS;
  557. if (data->als_enabled)
  558. state |= STK3310_STATE_EN_ALS;
  559. return stk3310_set_state(data, state);
  560. }
  561. static SIMPLE_DEV_PM_OPS(stk3310_pm_ops, stk3310_suspend, stk3310_resume);
  562. #define STK3310_PM_OPS (&stk3310_pm_ops)
  563. #else
  564. #define STK3310_PM_OPS NULL
  565. #endif
  566. static const struct i2c_device_id stk3310_i2c_id[] = {
  567. {"STK3310", 0},
  568. {"STK3311", 0},
  569. {}
  570. };
  571. MODULE_DEVICE_TABLE(i2c, stk3310_i2c_id);
  572. static const struct acpi_device_id stk3310_acpi_id[] = {
  573. {"STK3310", 0},
  574. {"STK3311", 0},
  575. {}
  576. };
  577. MODULE_DEVICE_TABLE(acpi, stk3310_acpi_id);
  578. static struct i2c_driver stk3310_driver = {
  579. .driver = {
  580. .name = "stk3310",
  581. .pm = STK3310_PM_OPS,
  582. .acpi_match_table = ACPI_PTR(stk3310_acpi_id),
  583. },
  584. .probe = stk3310_probe,
  585. .remove = stk3310_remove,
  586. .id_table = stk3310_i2c_id,
  587. };
  588. module_i2c_driver(stk3310_driver);
  589. MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>");
  590. MODULE_DESCRIPTION("STK3310 Ambient Light and Proximity Sensor driver");
  591. MODULE_LICENSE("GPL v2");