stk3310.c 18 KB

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