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