stk3310.c 19 KB

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