cm36651.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2013 Samsung Electronics Co., Ltd.
  4. * Author: Beomho Seo <[email protected]>
  5. */
  6. #include <linux/delay.h>
  7. #include <linux/err.h>
  8. #include <linux/i2c.h>
  9. #include <linux/mutex.h>
  10. #include <linux/module.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/regulator/consumer.h>
  13. #include <linux/iio/iio.h>
  14. #include <linux/iio/sysfs.h>
  15. #include <linux/iio/events.h>
  16. /* Slave address 0x19 for PS of 7 bit addressing protocol for I2C */
  17. #define CM36651_I2C_ADDR_PS 0x19
  18. /* Alert Response Address */
  19. #define CM36651_ARA 0x0C
  20. /* Ambient light sensor */
  21. #define CM36651_CS_CONF1 0x00
  22. #define CM36651_CS_CONF2 0x01
  23. #define CM36651_ALS_WH_M 0x02
  24. #define CM36651_ALS_WH_L 0x03
  25. #define CM36651_ALS_WL_M 0x04
  26. #define CM36651_ALS_WL_L 0x05
  27. #define CM36651_CS_CONF3 0x06
  28. #define CM36651_CS_CONF_REG_NUM 0x02
  29. /* Proximity sensor */
  30. #define CM36651_PS_CONF1 0x00
  31. #define CM36651_PS_THD 0x01
  32. #define CM36651_PS_CANC 0x02
  33. #define CM36651_PS_CONF2 0x03
  34. #define CM36651_PS_REG_NUM 0x04
  35. /* CS_CONF1 command code */
  36. #define CM36651_ALS_ENABLE 0x00
  37. #define CM36651_ALS_DISABLE 0x01
  38. #define CM36651_ALS_INT_EN 0x02
  39. #define CM36651_ALS_THRES 0x04
  40. /* CS_CONF2 command code */
  41. #define CM36651_CS_CONF2_DEFAULT_BIT 0x08
  42. /* CS_CONF3 channel integration time */
  43. #define CM36651_CS_IT1 0x00 /* Integration time 80 msec */
  44. #define CM36651_CS_IT2 0x40 /* Integration time 160 msec */
  45. #define CM36651_CS_IT3 0x80 /* Integration time 320 msec */
  46. #define CM36651_CS_IT4 0xC0 /* Integration time 640 msec */
  47. /* PS_CONF1 command code */
  48. #define CM36651_PS_ENABLE 0x00
  49. #define CM36651_PS_DISABLE 0x01
  50. #define CM36651_PS_INT_EN 0x02
  51. #define CM36651_PS_PERS2 0x04
  52. #define CM36651_PS_PERS3 0x08
  53. #define CM36651_PS_PERS4 0x0C
  54. /* PS_CONF1 command code: integration time */
  55. #define CM36651_PS_IT1 0x00 /* Integration time 0.32 msec */
  56. #define CM36651_PS_IT2 0x10 /* Integration time 0.42 msec */
  57. #define CM36651_PS_IT3 0x20 /* Integration time 0.52 msec */
  58. #define CM36651_PS_IT4 0x30 /* Integration time 0.64 msec */
  59. /* PS_CONF1 command code: duty ratio */
  60. #define CM36651_PS_DR1 0x00 /* Duty ratio 1/80 */
  61. #define CM36651_PS_DR2 0x40 /* Duty ratio 1/160 */
  62. #define CM36651_PS_DR3 0x80 /* Duty ratio 1/320 */
  63. #define CM36651_PS_DR4 0xC0 /* Duty ratio 1/640 */
  64. /* PS_THD command code */
  65. #define CM36651_PS_INITIAL_THD 0x05
  66. /* PS_CANC command code */
  67. #define CM36651_PS_CANC_DEFAULT 0x00
  68. /* PS_CONF2 command code */
  69. #define CM36651_PS_HYS1 0x00
  70. #define CM36651_PS_HYS2 0x01
  71. #define CM36651_PS_SMART_PERS_EN 0x02
  72. #define CM36651_PS_DIR_INT 0x04
  73. #define CM36651_PS_MS 0x10
  74. #define CM36651_CS_COLOR_NUM 4
  75. #define CM36651_CLOSE_PROXIMITY 0x32
  76. #define CM36651_FAR_PROXIMITY 0x33
  77. #define CM36651_CS_INT_TIME_AVAIL "0.08 0.16 0.32 0.64"
  78. #define CM36651_PS_INT_TIME_AVAIL "0.000320 0.000420 0.000520 0.000640"
  79. enum cm36651_operation_mode {
  80. CM36651_LIGHT_EN,
  81. CM36651_PROXIMITY_EN,
  82. CM36651_PROXIMITY_EV_EN,
  83. };
  84. enum cm36651_light_channel_idx {
  85. CM36651_LIGHT_CHANNEL_IDX_RED,
  86. CM36651_LIGHT_CHANNEL_IDX_GREEN,
  87. CM36651_LIGHT_CHANNEL_IDX_BLUE,
  88. CM36651_LIGHT_CHANNEL_IDX_CLEAR,
  89. };
  90. enum cm36651_command {
  91. CM36651_CMD_READ_RAW_LIGHT,
  92. CM36651_CMD_READ_RAW_PROXIMITY,
  93. CM36651_CMD_PROX_EV_EN,
  94. CM36651_CMD_PROX_EV_DIS,
  95. };
  96. static const u8 cm36651_cs_reg[CM36651_CS_CONF_REG_NUM] = {
  97. CM36651_CS_CONF1,
  98. CM36651_CS_CONF2,
  99. };
  100. static const u8 cm36651_ps_reg[CM36651_PS_REG_NUM] = {
  101. CM36651_PS_CONF1,
  102. CM36651_PS_THD,
  103. CM36651_PS_CANC,
  104. CM36651_PS_CONF2,
  105. };
  106. struct cm36651_data {
  107. const struct cm36651_platform_data *pdata;
  108. struct i2c_client *client;
  109. struct i2c_client *ps_client;
  110. struct i2c_client *ara_client;
  111. struct mutex lock;
  112. struct regulator *vled_reg;
  113. unsigned long flags;
  114. int cs_int_time[CM36651_CS_COLOR_NUM];
  115. int ps_int_time;
  116. u8 cs_ctrl_regs[CM36651_CS_CONF_REG_NUM];
  117. u8 ps_ctrl_regs[CM36651_PS_REG_NUM];
  118. u16 color[CM36651_CS_COLOR_NUM];
  119. };
  120. static int cm36651_setup_reg(struct cm36651_data *cm36651)
  121. {
  122. struct i2c_client *client = cm36651->client;
  123. struct i2c_client *ps_client = cm36651->ps_client;
  124. int i, ret;
  125. /* CS initialization */
  126. cm36651->cs_ctrl_regs[CM36651_CS_CONF1] = CM36651_ALS_ENABLE |
  127. CM36651_ALS_THRES;
  128. cm36651->cs_ctrl_regs[CM36651_CS_CONF2] = CM36651_CS_CONF2_DEFAULT_BIT;
  129. for (i = 0; i < CM36651_CS_CONF_REG_NUM; i++) {
  130. ret = i2c_smbus_write_byte_data(client, cm36651_cs_reg[i],
  131. cm36651->cs_ctrl_regs[i]);
  132. if (ret < 0)
  133. return ret;
  134. }
  135. /* PS initialization */
  136. cm36651->ps_ctrl_regs[CM36651_PS_CONF1] = CM36651_PS_ENABLE |
  137. CM36651_PS_IT2;
  138. cm36651->ps_ctrl_regs[CM36651_PS_THD] = CM36651_PS_INITIAL_THD;
  139. cm36651->ps_ctrl_regs[CM36651_PS_CANC] = CM36651_PS_CANC_DEFAULT;
  140. cm36651->ps_ctrl_regs[CM36651_PS_CONF2] = CM36651_PS_HYS2 |
  141. CM36651_PS_DIR_INT | CM36651_PS_SMART_PERS_EN;
  142. for (i = 0; i < CM36651_PS_REG_NUM; i++) {
  143. ret = i2c_smbus_write_byte_data(ps_client, cm36651_ps_reg[i],
  144. cm36651->ps_ctrl_regs[i]);
  145. if (ret < 0)
  146. return ret;
  147. }
  148. /* Set shutdown mode */
  149. ret = i2c_smbus_write_byte_data(client, CM36651_CS_CONF1,
  150. CM36651_ALS_DISABLE);
  151. if (ret < 0)
  152. return ret;
  153. ret = i2c_smbus_write_byte_data(cm36651->ps_client,
  154. CM36651_PS_CONF1, CM36651_PS_DISABLE);
  155. if (ret < 0)
  156. return ret;
  157. return 0;
  158. }
  159. static int cm36651_read_output(struct cm36651_data *cm36651,
  160. struct iio_chan_spec const *chan, int *val)
  161. {
  162. struct i2c_client *client = cm36651->client;
  163. int ret = -EINVAL;
  164. switch (chan->type) {
  165. case IIO_LIGHT:
  166. *val = i2c_smbus_read_word_data(client, chan->address);
  167. if (*val < 0)
  168. return ret;
  169. ret = i2c_smbus_write_byte_data(client, CM36651_CS_CONF1,
  170. CM36651_ALS_DISABLE);
  171. if (ret < 0)
  172. return ret;
  173. ret = IIO_VAL_INT;
  174. break;
  175. case IIO_PROXIMITY:
  176. *val = i2c_smbus_read_byte(cm36651->ps_client);
  177. if (*val < 0)
  178. return ret;
  179. if (!test_bit(CM36651_PROXIMITY_EV_EN, &cm36651->flags)) {
  180. ret = i2c_smbus_write_byte_data(cm36651->ps_client,
  181. CM36651_PS_CONF1, CM36651_PS_DISABLE);
  182. if (ret < 0)
  183. return ret;
  184. }
  185. ret = IIO_VAL_INT;
  186. break;
  187. default:
  188. break;
  189. }
  190. return ret;
  191. }
  192. static irqreturn_t cm36651_irq_handler(int irq, void *data)
  193. {
  194. struct iio_dev *indio_dev = data;
  195. struct cm36651_data *cm36651 = iio_priv(indio_dev);
  196. struct i2c_client *client = cm36651->client;
  197. int ev_dir, ret;
  198. u64 ev_code;
  199. /*
  200. * The PS INT pin is an active low signal that PS INT move logic low
  201. * when the object is detect. Once the MCU host received the PS INT
  202. * "LOW" signal, the Host needs to read the data at Alert Response
  203. * Address(ARA) to clear the PS INT signal. After clearing the PS
  204. * INT pin, the PS INT signal toggles from low to high.
  205. */
  206. ret = i2c_smbus_read_byte(cm36651->ara_client);
  207. if (ret < 0) {
  208. dev_err(&client->dev,
  209. "%s: Data read failed: %d\n", __func__, ret);
  210. return IRQ_HANDLED;
  211. }
  212. switch (ret) {
  213. case CM36651_CLOSE_PROXIMITY:
  214. ev_dir = IIO_EV_DIR_RISING;
  215. break;
  216. case CM36651_FAR_PROXIMITY:
  217. ev_dir = IIO_EV_DIR_FALLING;
  218. break;
  219. default:
  220. dev_err(&client->dev,
  221. "%s: Data read wrong: %d\n", __func__, ret);
  222. return IRQ_HANDLED;
  223. }
  224. ev_code = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY,
  225. CM36651_CMD_READ_RAW_PROXIMITY,
  226. IIO_EV_TYPE_THRESH, ev_dir);
  227. iio_push_event(indio_dev, ev_code, iio_get_time_ns(indio_dev));
  228. return IRQ_HANDLED;
  229. }
  230. static int cm36651_set_operation_mode(struct cm36651_data *cm36651, int cmd)
  231. {
  232. struct i2c_client *client = cm36651->client;
  233. struct i2c_client *ps_client = cm36651->ps_client;
  234. int ret = -EINVAL;
  235. switch (cmd) {
  236. case CM36651_CMD_READ_RAW_LIGHT:
  237. ret = i2c_smbus_write_byte_data(client, CM36651_CS_CONF1,
  238. cm36651->cs_ctrl_regs[CM36651_CS_CONF1]);
  239. break;
  240. case CM36651_CMD_READ_RAW_PROXIMITY:
  241. if (test_bit(CM36651_PROXIMITY_EV_EN, &cm36651->flags))
  242. return CM36651_PROXIMITY_EV_EN;
  243. ret = i2c_smbus_write_byte_data(ps_client, CM36651_PS_CONF1,
  244. cm36651->ps_ctrl_regs[CM36651_PS_CONF1]);
  245. break;
  246. case CM36651_CMD_PROX_EV_EN:
  247. if (test_bit(CM36651_PROXIMITY_EV_EN, &cm36651->flags)) {
  248. dev_err(&client->dev,
  249. "Already proximity event enable state\n");
  250. return ret;
  251. }
  252. set_bit(CM36651_PROXIMITY_EV_EN, &cm36651->flags);
  253. ret = i2c_smbus_write_byte_data(ps_client,
  254. cm36651_ps_reg[CM36651_PS_CONF1],
  255. CM36651_PS_INT_EN | CM36651_PS_PERS2 | CM36651_PS_IT2);
  256. if (ret < 0) {
  257. dev_err(&client->dev, "Proximity enable event failed\n");
  258. return ret;
  259. }
  260. break;
  261. case CM36651_CMD_PROX_EV_DIS:
  262. if (!test_bit(CM36651_PROXIMITY_EV_EN, &cm36651->flags)) {
  263. dev_err(&client->dev,
  264. "Already proximity event disable state\n");
  265. return ret;
  266. }
  267. clear_bit(CM36651_PROXIMITY_EV_EN, &cm36651->flags);
  268. ret = i2c_smbus_write_byte_data(ps_client,
  269. CM36651_PS_CONF1, CM36651_PS_DISABLE);
  270. break;
  271. }
  272. if (ret < 0)
  273. dev_err(&client->dev, "Write register failed\n");
  274. return ret;
  275. }
  276. static int cm36651_read_channel(struct cm36651_data *cm36651,
  277. struct iio_chan_spec const *chan, int *val)
  278. {
  279. struct i2c_client *client = cm36651->client;
  280. int cmd, ret;
  281. if (chan->type == IIO_LIGHT)
  282. cmd = CM36651_CMD_READ_RAW_LIGHT;
  283. else if (chan->type == IIO_PROXIMITY)
  284. cmd = CM36651_CMD_READ_RAW_PROXIMITY;
  285. else
  286. return -EINVAL;
  287. ret = cm36651_set_operation_mode(cm36651, cmd);
  288. if (ret < 0) {
  289. dev_err(&client->dev, "CM36651 set operation mode failed\n");
  290. return ret;
  291. }
  292. /* Delay for work after enable operation */
  293. msleep(50);
  294. ret = cm36651_read_output(cm36651, chan, val);
  295. if (ret < 0) {
  296. dev_err(&client->dev, "CM36651 read output failed\n");
  297. return ret;
  298. }
  299. return ret;
  300. }
  301. static int cm36651_read_int_time(struct cm36651_data *cm36651,
  302. struct iio_chan_spec const *chan, int *val2)
  303. {
  304. switch (chan->type) {
  305. case IIO_LIGHT:
  306. if (cm36651->cs_int_time[chan->address] == CM36651_CS_IT1)
  307. *val2 = 80000;
  308. else if (cm36651->cs_int_time[chan->address] == CM36651_CS_IT2)
  309. *val2 = 160000;
  310. else if (cm36651->cs_int_time[chan->address] == CM36651_CS_IT3)
  311. *val2 = 320000;
  312. else if (cm36651->cs_int_time[chan->address] == CM36651_CS_IT4)
  313. *val2 = 640000;
  314. else
  315. return -EINVAL;
  316. break;
  317. case IIO_PROXIMITY:
  318. if (cm36651->ps_int_time == CM36651_PS_IT1)
  319. *val2 = 320;
  320. else if (cm36651->ps_int_time == CM36651_PS_IT2)
  321. *val2 = 420;
  322. else if (cm36651->ps_int_time == CM36651_PS_IT3)
  323. *val2 = 520;
  324. else if (cm36651->ps_int_time == CM36651_PS_IT4)
  325. *val2 = 640;
  326. else
  327. return -EINVAL;
  328. break;
  329. default:
  330. return -EINVAL;
  331. }
  332. return IIO_VAL_INT_PLUS_MICRO;
  333. }
  334. static int cm36651_write_int_time(struct cm36651_data *cm36651,
  335. struct iio_chan_spec const *chan, int val)
  336. {
  337. struct i2c_client *client = cm36651->client;
  338. struct i2c_client *ps_client = cm36651->ps_client;
  339. int int_time, ret;
  340. switch (chan->type) {
  341. case IIO_LIGHT:
  342. if (val == 80000)
  343. int_time = CM36651_CS_IT1;
  344. else if (val == 160000)
  345. int_time = CM36651_CS_IT2;
  346. else if (val == 320000)
  347. int_time = CM36651_CS_IT3;
  348. else if (val == 640000)
  349. int_time = CM36651_CS_IT4;
  350. else
  351. return -EINVAL;
  352. ret = i2c_smbus_write_byte_data(client, CM36651_CS_CONF3,
  353. int_time >> 2 * (chan->address));
  354. if (ret < 0) {
  355. dev_err(&client->dev, "CS integration time write failed\n");
  356. return ret;
  357. }
  358. cm36651->cs_int_time[chan->address] = int_time;
  359. break;
  360. case IIO_PROXIMITY:
  361. if (val == 320)
  362. int_time = CM36651_PS_IT1;
  363. else if (val == 420)
  364. int_time = CM36651_PS_IT2;
  365. else if (val == 520)
  366. int_time = CM36651_PS_IT3;
  367. else if (val == 640)
  368. int_time = CM36651_PS_IT4;
  369. else
  370. return -EINVAL;
  371. ret = i2c_smbus_write_byte_data(ps_client,
  372. CM36651_PS_CONF1, int_time);
  373. if (ret < 0) {
  374. dev_err(&client->dev, "PS integration time write failed\n");
  375. return ret;
  376. }
  377. cm36651->ps_int_time = int_time;
  378. break;
  379. default:
  380. return -EINVAL;
  381. }
  382. return ret;
  383. }
  384. static int cm36651_read_raw(struct iio_dev *indio_dev,
  385. struct iio_chan_spec const *chan,
  386. int *val, int *val2, long mask)
  387. {
  388. struct cm36651_data *cm36651 = iio_priv(indio_dev);
  389. int ret;
  390. mutex_lock(&cm36651->lock);
  391. switch (mask) {
  392. case IIO_CHAN_INFO_RAW:
  393. ret = cm36651_read_channel(cm36651, chan, val);
  394. break;
  395. case IIO_CHAN_INFO_INT_TIME:
  396. *val = 0;
  397. ret = cm36651_read_int_time(cm36651, chan, val2);
  398. break;
  399. default:
  400. ret = -EINVAL;
  401. }
  402. mutex_unlock(&cm36651->lock);
  403. return ret;
  404. }
  405. static int cm36651_write_raw(struct iio_dev *indio_dev,
  406. struct iio_chan_spec const *chan,
  407. int val, int val2, long mask)
  408. {
  409. struct cm36651_data *cm36651 = iio_priv(indio_dev);
  410. struct i2c_client *client = cm36651->client;
  411. int ret = -EINVAL;
  412. if (mask == IIO_CHAN_INFO_INT_TIME) {
  413. ret = cm36651_write_int_time(cm36651, chan, val2);
  414. if (ret < 0)
  415. dev_err(&client->dev, "Integration time write failed\n");
  416. }
  417. return ret;
  418. }
  419. static int cm36651_read_prox_thresh(struct iio_dev *indio_dev,
  420. const struct iio_chan_spec *chan,
  421. enum iio_event_type type,
  422. enum iio_event_direction dir,
  423. enum iio_event_info info,
  424. int *val, int *val2)
  425. {
  426. struct cm36651_data *cm36651 = iio_priv(indio_dev);
  427. *val = cm36651->ps_ctrl_regs[CM36651_PS_THD];
  428. return 0;
  429. }
  430. static int cm36651_write_prox_thresh(struct iio_dev *indio_dev,
  431. const struct iio_chan_spec *chan,
  432. enum iio_event_type type,
  433. enum iio_event_direction dir,
  434. enum iio_event_info info,
  435. int val, int val2)
  436. {
  437. struct cm36651_data *cm36651 = iio_priv(indio_dev);
  438. struct i2c_client *client = cm36651->client;
  439. int ret;
  440. if (val < 3 || val > 255)
  441. return -EINVAL;
  442. cm36651->ps_ctrl_regs[CM36651_PS_THD] = val;
  443. ret = i2c_smbus_write_byte_data(cm36651->ps_client, CM36651_PS_THD,
  444. cm36651->ps_ctrl_regs[CM36651_PS_THD]);
  445. if (ret < 0) {
  446. dev_err(&client->dev, "PS threshold write failed: %d\n", ret);
  447. return ret;
  448. }
  449. return 0;
  450. }
  451. static int cm36651_write_prox_event_config(struct iio_dev *indio_dev,
  452. const struct iio_chan_spec *chan,
  453. enum iio_event_type type,
  454. enum iio_event_direction dir,
  455. int state)
  456. {
  457. struct cm36651_data *cm36651 = iio_priv(indio_dev);
  458. int cmd, ret;
  459. mutex_lock(&cm36651->lock);
  460. cmd = state ? CM36651_CMD_PROX_EV_EN : CM36651_CMD_PROX_EV_DIS;
  461. ret = cm36651_set_operation_mode(cm36651, cmd);
  462. mutex_unlock(&cm36651->lock);
  463. return ret;
  464. }
  465. static int cm36651_read_prox_event_config(struct iio_dev *indio_dev,
  466. const struct iio_chan_spec *chan,
  467. enum iio_event_type type,
  468. enum iio_event_direction dir)
  469. {
  470. struct cm36651_data *cm36651 = iio_priv(indio_dev);
  471. int event_en;
  472. mutex_lock(&cm36651->lock);
  473. event_en = test_bit(CM36651_PROXIMITY_EV_EN, &cm36651->flags);
  474. mutex_unlock(&cm36651->lock);
  475. return event_en;
  476. }
  477. #define CM36651_LIGHT_CHANNEL(_color, _idx) { \
  478. .type = IIO_LIGHT, \
  479. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  480. BIT(IIO_CHAN_INFO_INT_TIME), \
  481. .address = _idx, \
  482. .modified = 1, \
  483. .channel2 = IIO_MOD_LIGHT_##_color, \
  484. } \
  485. static const struct iio_event_spec cm36651_event_spec[] = {
  486. {
  487. .type = IIO_EV_TYPE_THRESH,
  488. .dir = IIO_EV_DIR_EITHER,
  489. .mask_separate = BIT(IIO_EV_INFO_VALUE) |
  490. BIT(IIO_EV_INFO_ENABLE),
  491. }
  492. };
  493. static const struct iio_chan_spec cm36651_channels[] = {
  494. {
  495. .type = IIO_PROXIMITY,
  496. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  497. BIT(IIO_CHAN_INFO_INT_TIME),
  498. .event_spec = cm36651_event_spec,
  499. .num_event_specs = ARRAY_SIZE(cm36651_event_spec),
  500. },
  501. CM36651_LIGHT_CHANNEL(RED, CM36651_LIGHT_CHANNEL_IDX_RED),
  502. CM36651_LIGHT_CHANNEL(GREEN, CM36651_LIGHT_CHANNEL_IDX_GREEN),
  503. CM36651_LIGHT_CHANNEL(BLUE, CM36651_LIGHT_CHANNEL_IDX_BLUE),
  504. CM36651_LIGHT_CHANNEL(CLEAR, CM36651_LIGHT_CHANNEL_IDX_CLEAR),
  505. };
  506. static IIO_CONST_ATTR(in_illuminance_integration_time_available,
  507. CM36651_CS_INT_TIME_AVAIL);
  508. static IIO_CONST_ATTR(in_proximity_integration_time_available,
  509. CM36651_PS_INT_TIME_AVAIL);
  510. static struct attribute *cm36651_attributes[] = {
  511. &iio_const_attr_in_illuminance_integration_time_available.dev_attr.attr,
  512. &iio_const_attr_in_proximity_integration_time_available.dev_attr.attr,
  513. NULL,
  514. };
  515. static const struct attribute_group cm36651_attribute_group = {
  516. .attrs = cm36651_attributes
  517. };
  518. static const struct iio_info cm36651_info = {
  519. .read_raw = &cm36651_read_raw,
  520. .write_raw = &cm36651_write_raw,
  521. .read_event_value = &cm36651_read_prox_thresh,
  522. .write_event_value = &cm36651_write_prox_thresh,
  523. .read_event_config = &cm36651_read_prox_event_config,
  524. .write_event_config = &cm36651_write_prox_event_config,
  525. .attrs = &cm36651_attribute_group,
  526. };
  527. static int cm36651_probe(struct i2c_client *client,
  528. const struct i2c_device_id *id)
  529. {
  530. struct cm36651_data *cm36651;
  531. struct iio_dev *indio_dev;
  532. int ret;
  533. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*cm36651));
  534. if (!indio_dev)
  535. return -ENOMEM;
  536. cm36651 = iio_priv(indio_dev);
  537. cm36651->vled_reg = devm_regulator_get(&client->dev, "vled");
  538. if (IS_ERR(cm36651->vled_reg))
  539. return dev_err_probe(&client->dev, PTR_ERR(cm36651->vled_reg),
  540. "get regulator vled failed\n");
  541. ret = regulator_enable(cm36651->vled_reg);
  542. if (ret) {
  543. dev_err(&client->dev, "enable regulator vled failed\n");
  544. return ret;
  545. }
  546. i2c_set_clientdata(client, indio_dev);
  547. cm36651->client = client;
  548. cm36651->ps_client = i2c_new_dummy_device(client->adapter,
  549. CM36651_I2C_ADDR_PS);
  550. if (IS_ERR(cm36651->ps_client)) {
  551. dev_err(&client->dev, "%s: new i2c device failed\n", __func__);
  552. ret = PTR_ERR(cm36651->ps_client);
  553. goto error_disable_reg;
  554. }
  555. cm36651->ara_client = i2c_new_dummy_device(client->adapter, CM36651_ARA);
  556. if (IS_ERR(cm36651->ara_client)) {
  557. dev_err(&client->dev, "%s: new i2c device failed\n", __func__);
  558. ret = PTR_ERR(cm36651->ara_client);
  559. goto error_i2c_unregister_ps;
  560. }
  561. mutex_init(&cm36651->lock);
  562. indio_dev->channels = cm36651_channels;
  563. indio_dev->num_channels = ARRAY_SIZE(cm36651_channels);
  564. indio_dev->info = &cm36651_info;
  565. indio_dev->name = id->name;
  566. indio_dev->modes = INDIO_DIRECT_MODE;
  567. ret = cm36651_setup_reg(cm36651);
  568. if (ret) {
  569. dev_err(&client->dev, "%s: register setup failed\n", __func__);
  570. goto error_i2c_unregister_ara;
  571. }
  572. ret = request_threaded_irq(client->irq, NULL, cm36651_irq_handler,
  573. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  574. "cm36651", indio_dev);
  575. if (ret) {
  576. dev_err(&client->dev, "%s: request irq failed\n", __func__);
  577. goto error_i2c_unregister_ara;
  578. }
  579. ret = iio_device_register(indio_dev);
  580. if (ret) {
  581. dev_err(&client->dev, "%s: regist device failed\n", __func__);
  582. goto error_free_irq;
  583. }
  584. return 0;
  585. error_free_irq:
  586. free_irq(client->irq, indio_dev);
  587. error_i2c_unregister_ara:
  588. i2c_unregister_device(cm36651->ara_client);
  589. error_i2c_unregister_ps:
  590. i2c_unregister_device(cm36651->ps_client);
  591. error_disable_reg:
  592. regulator_disable(cm36651->vled_reg);
  593. return ret;
  594. }
  595. static void cm36651_remove(struct i2c_client *client)
  596. {
  597. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  598. struct cm36651_data *cm36651 = iio_priv(indio_dev);
  599. iio_device_unregister(indio_dev);
  600. regulator_disable(cm36651->vled_reg);
  601. free_irq(client->irq, indio_dev);
  602. i2c_unregister_device(cm36651->ps_client);
  603. i2c_unregister_device(cm36651->ara_client);
  604. }
  605. static const struct i2c_device_id cm36651_id[] = {
  606. { "cm36651", 0 },
  607. { }
  608. };
  609. MODULE_DEVICE_TABLE(i2c, cm36651_id);
  610. static const struct of_device_id cm36651_of_match[] = {
  611. { .compatible = "capella,cm36651" },
  612. { }
  613. };
  614. MODULE_DEVICE_TABLE(of, cm36651_of_match);
  615. static struct i2c_driver cm36651_driver = {
  616. .driver = {
  617. .name = "cm36651",
  618. .of_match_table = cm36651_of_match,
  619. },
  620. .probe = cm36651_probe,
  621. .remove = cm36651_remove,
  622. .id_table = cm36651_id,
  623. };
  624. module_i2c_driver(cm36651_driver);
  625. MODULE_AUTHOR("Beomho Seo <[email protected]>");
  626. MODULE_DESCRIPTION("CM36651 proximity/ambient light sensor driver");
  627. MODULE_LICENSE("GPL v2");