cm3323.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /*
  2. * Copyright (C) 2013 Samsung Electronics. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  16. * 02110-1301 USA
  17. */
  18. #include <linux/module.h>
  19. #include <linux/i2c.h>
  20. #include <linux/errno.h>
  21. #include <linux/input.h>
  22. #include <linux/workqueue.h>
  23. #include <linux/slab.h>
  24. #include <linux/delay.h>
  25. #include <linux/of_gpio.h>
  26. #include <linux/regulator/consumer.h>
  27. #include "sensors_core.h"
  28. #define VENDOR_NAME "CAPELLA"
  29. #define MODEL_NAME "CM3323"
  30. #define MODULE_NAME "light_sensor"
  31. #define I2C_M_WR 0 /* for i2c Write */
  32. #define I2c_M_RD 1 /* for i2c Read */
  33. #define REL_RED REL_HWHEEL
  34. #define REL_GREEN REL_DIAL
  35. #define REL_BLUE REL_WHEEL
  36. #define REL_WHITE REL_MISC
  37. /* register addresses */
  38. /* Ambient light sensor */
  39. #define REG_CS_CONF1 0x00
  40. #define REG_RED 0x08
  41. #define REG_GREEN 0x09
  42. #define REG_BLUE 0x0A
  43. #define REG_WHITE 0x0B
  44. #define LIGHT_LOG_TIME 15 /* 15 sec */
  45. #define ALS_REG_NUM 2
  46. enum {
  47. LIGHT_ENABLED = BIT(0),
  48. PROXIMITY_ENABLED = BIT(1),
  49. };
  50. /* register settings */
  51. static const u8 als_reg_setting[ALS_REG_NUM][2] = {
  52. {REG_CS_CONF1, 0x00}, /* enable */
  53. {REG_CS_CONF1, 0x01}, /* disable */
  54. };
  55. #define CM3323_DEFAULT_DELAY 200000000LL
  56. /* driver data */
  57. struct cm3323_p {
  58. struct i2c_client *i2c_client;
  59. struct input_dev *input;
  60. struct device *light_dev;
  61. struct delayed_work work;
  62. atomic_t delay;
  63. u8 power_state;
  64. u16 color[4];
  65. int irq;
  66. int time_count;
  67. #ifdef CONFIG_SEC_RUBENS_PROJECT
  68. struct regulator *vdd_2p85;
  69. #endif
  70. u64 timestamp;
  71. };
  72. #ifdef CONFIG_SEC_RUBENS_PROJECT
  73. static void sensor_power_on_vdd(struct cm3323_p *info, int onoff)
  74. {
  75. int ret;
  76. if (info->vdd_2p85 == NULL) {
  77. info->vdd_2p85 =regulator_get(&info->i2c_client->dev, "8226_l19");
  78. if (IS_ERR(info->vdd_2p85)){
  79. pr_err("%s: regulator_get failed for 8226_l19\n", __func__);
  80. return ;
  81. }
  82. ret = regulator_set_voltage(info->vdd_2p85, 2850000, 2850000);
  83. if (ret)
  84. pr_err("%s: error vsensor_2p85 setting voltage ret=%d\n",__func__, ret);
  85. }
  86. if (onoff == 1) {
  87. ret = regulator_enable(info->vdd_2p85);
  88. if (ret)
  89. pr_err("%s: error enablinig regulator info->vdd_2p85\n", __func__);
  90. }
  91. else if (onoff == 0) {
  92. if (regulator_is_enabled(info->vdd_2p85)) {
  93. ret = regulator_disable(info->vdd_2p85);
  94. if (ret)
  95. pr_err("%s: error vdd_2p85 disabling regulator\n",__func__);
  96. }
  97. }
  98. msleep(30);
  99. return;
  100. }
  101. #endif
  102. static int cm3323_i2c_read_word(struct cm3323_p *data,
  103. unsigned char reg_addr, u16 *buf)
  104. {
  105. int ret;
  106. struct i2c_msg msg[2];
  107. unsigned char r_buf[2];
  108. msg[0].addr = data->i2c_client->addr;
  109. msg[0].flags = I2C_M_WR;
  110. msg[0].len = 1;
  111. msg[0].buf = &reg_addr;
  112. msg[1].addr = data->i2c_client->addr;
  113. msg[1].flags = I2C_M_RD;
  114. msg[1].len = 2;
  115. msg[1].buf = r_buf;
  116. ret = i2c_transfer(data->i2c_client->adapter, msg, 2);
  117. if (ret < 0) {
  118. pr_err("[SENSOR]: %s - i2c read error %d\n", __func__, ret);
  119. return ret;
  120. }
  121. *buf = (u16)r_buf[1];
  122. *buf = (*buf << 8) | (u16)r_buf[0];
  123. return 0;
  124. }
  125. static int cm3323_i2c_write(struct cm3323_p *data,
  126. unsigned char reg_addr, unsigned char buf)
  127. {
  128. int ret = 0;
  129. struct i2c_msg msg;
  130. unsigned char w_buf[2];
  131. w_buf[0] = reg_addr;
  132. w_buf[1] = buf;
  133. /* send slave address & command */
  134. msg.addr = data->i2c_client->addr;
  135. msg.flags = I2C_M_WR;
  136. msg.len = 2;
  137. msg.buf = (char *)w_buf;
  138. ret = i2c_transfer(data->i2c_client->adapter, &msg, 1);
  139. if (ret < 0) {
  140. pr_err("[SENSOR]: %s - i2c write error %d\n", __func__, ret);
  141. return ret;
  142. }
  143. return 0;
  144. }
  145. static void cm3323_light_enable(struct cm3323_p *data)
  146. {
  147. cm3323_i2c_write(data, REG_CS_CONF1, als_reg_setting[0][1]);
  148. schedule_delayed_work(&data->work,
  149. nsecs_to_jiffies(atomic_read(&data->delay)));
  150. }
  151. static void cm3323_light_disable(struct cm3323_p *data)
  152. {
  153. /* disable setting */
  154. cm3323_i2c_write(data, REG_CS_CONF1, als_reg_setting[1][1]);
  155. cancel_delayed_work_sync(&data->work);
  156. }
  157. static void cm3323_work_func_light(struct work_struct *work)
  158. {
  159. struct timespec ts;
  160. int time_hi, time_lo;
  161. struct cm3323_p *data = container_of((struct delayed_work *)work,
  162. struct cm3323_p, work);
  163. unsigned long delay = nsecs_to_jiffies(atomic_read(&data->delay));
  164. get_monotonic_boottime(&ts);
  165. data->timestamp = ts.tv_sec * 1000000000ULL + ts.tv_nsec;
  166. time_lo = (int)(data->timestamp & TIME_LO_MASK);
  167. time_hi = (int)((data->timestamp & TIME_HI_MASK) >> TIME_HI_SHIFT);
  168. cm3323_i2c_read_word(data, REG_RED, &data->color[0]);
  169. cm3323_i2c_read_word(data, REG_GREEN, &data->color[1]);
  170. cm3323_i2c_read_word(data, REG_BLUE, &data->color[2]);
  171. cm3323_i2c_read_word(data, REG_WHITE, &data->color[3]);
  172. input_report_rel(data->input, REL_RED, data->color[0] + 1);
  173. input_report_rel(data->input, REL_GREEN, data->color[1] + 1);
  174. input_report_rel(data->input, REL_BLUE, data->color[2] + 1);
  175. input_report_rel(data->input, REL_WHITE, data->color[3] + 1);
  176. input_report_rel(data->input, REL_X, time_hi);
  177. input_report_rel(data->input, REL_Y, time_lo);
  178. input_sync(data->input);
  179. if (((int64_t)atomic_read(&data->delay) * (int64_t)data->time_count)
  180. >= ((int64_t)LIGHT_LOG_TIME * NSEC_PER_SEC)) {
  181. pr_info("[SENSOR]: %s - r = %u g = %u b = %u w = %u\n",
  182. __func__, data->color[0], data->color[1],
  183. data->color[2], data->color[3]);
  184. data->time_count = 0;
  185. } else {
  186. data->time_count++;
  187. }
  188. schedule_delayed_work(&data->work, delay);
  189. }
  190. /* sysfs */
  191. static ssize_t cm3323_poll_delay_show(struct device *dev,
  192. struct device_attribute *attr, char *buf)
  193. {
  194. struct cm3323_p *data = dev_get_drvdata(dev);
  195. return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&data->delay));
  196. }
  197. static ssize_t cm3323_poll_delay_store(struct device *dev,
  198. struct device_attribute *attr, const char *buf, size_t size)
  199. {
  200. struct cm3323_p *data = dev_get_drvdata(dev);
  201. int64_t new_delay;
  202. int ret;
  203. ret = kstrtoll(buf, 10, &new_delay);
  204. if (ret) {
  205. pr_err("[SENSOR]: %s - Invalid Argument\n", __func__);
  206. return ret;
  207. }
  208. atomic_set(&data->delay, new_delay);
  209. pr_info("[SENSOR]: %s - poll_delay = %lld\n", __func__, new_delay);
  210. return size;
  211. }
  212. static ssize_t light_enable_store(struct device *dev,
  213. struct device_attribute *attr, const char *buf, size_t size)
  214. {
  215. u8 enable;
  216. int ret;
  217. struct cm3323_p *data = dev_get_drvdata(dev);
  218. ret = kstrtou8(buf, 2, &enable);
  219. if (ret) {
  220. pr_err("[SENSOR]: %s - Invalid Argument\n", __func__);
  221. return ret;
  222. }
  223. pr_info("[SENSOR]: %s - new_value = %u\n", __func__, enable);
  224. if (enable && !(data->power_state & LIGHT_ENABLED)) {
  225. data->power_state |= LIGHT_ENABLED;
  226. cm3323_light_enable(data);
  227. } else if (!enable && (data->power_state & LIGHT_ENABLED)) {
  228. cm3323_light_disable(data);
  229. data->power_state &= ~LIGHT_ENABLED;
  230. }
  231. return size;
  232. }
  233. static ssize_t light_enable_show(struct device *dev,
  234. struct device_attribute *attr, char *buf)
  235. {
  236. struct cm3323_p *data = dev_get_drvdata(dev);
  237. return snprintf(buf, PAGE_SIZE, "%d\n",
  238. (data->power_state & LIGHT_ENABLED) ? 1 : 0);
  239. }
  240. static DEVICE_ATTR(poll_delay, S_IRUGO | S_IWUSR | S_IWGRP,
  241. cm3323_poll_delay_show, cm3323_poll_delay_store);
  242. static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR | S_IWGRP,
  243. light_enable_show, light_enable_store);
  244. static struct attribute *light_sysfs_attrs[] = {
  245. &dev_attr_enable.attr,
  246. &dev_attr_poll_delay.attr,
  247. NULL,
  248. };
  249. static struct attribute_group light_attribute_group = {
  250. .attrs = light_sysfs_attrs,
  251. };
  252. /* light sysfs */
  253. static ssize_t cm3323_name_show(struct device *dev,
  254. struct device_attribute *attr, char *buf)
  255. {
  256. return snprintf(buf, PAGE_SIZE, "%s\n", MODEL_NAME);
  257. }
  258. static ssize_t cm3323_vendor_show(struct device *dev,
  259. struct device_attribute *attr, char *buf)
  260. {
  261. return snprintf(buf, PAGE_SIZE, "%s\n", VENDOR_NAME);
  262. }
  263. static ssize_t light_lux_show(struct device *dev,
  264. struct device_attribute *attr, char *buf)
  265. {
  266. struct cm3323_p *data = dev_get_drvdata(dev);
  267. return snprintf(buf, PAGE_SIZE, "%u,%u,%u,%u\n",
  268. data->color[0], data->color[1],
  269. data->color[2], data->color[3]);
  270. }
  271. static ssize_t light_data_show(struct device *dev,
  272. struct device_attribute *attr, char *buf)
  273. {
  274. struct cm3323_p *data = dev_get_drvdata(dev);
  275. return snprintf(buf, PAGE_SIZE, "%u,%u,%u,%u\n",
  276. data->color[0], data->color[1],
  277. data->color[2], data->color[3]);
  278. }
  279. static DEVICE_ATTR(name, S_IRUGO, cm3323_name_show, NULL);
  280. static DEVICE_ATTR(vendor, S_IRUGO, cm3323_vendor_show, NULL);
  281. static DEVICE_ATTR(lux, S_IRUGO, light_lux_show, NULL);
  282. static DEVICE_ATTR(raw_data, S_IRUGO, light_data_show, NULL);
  283. static struct device_attribute *sensor_attrs[] = {
  284. &dev_attr_name,
  285. &dev_attr_vendor,
  286. &dev_attr_lux,
  287. &dev_attr_raw_data,
  288. NULL,
  289. };
  290. static int cm3323_setup_reg(struct cm3323_p *data)
  291. {
  292. int ret = 0;
  293. /* ALS initialization */
  294. ret = cm3323_i2c_write(data,
  295. als_reg_setting[0][0],
  296. als_reg_setting[0][1]);
  297. if (ret < 0) {
  298. pr_err("[SENSOR]: %s - cm3323_als_reg is failed. %d\n",
  299. __func__, ret);
  300. return ret;
  301. }
  302. /* turn off */
  303. cm3323_i2c_write(data, REG_CS_CONF1, 0x01);
  304. return ret;
  305. }
  306. static int cm3323_input_init(struct cm3323_p *data)
  307. {
  308. int ret = 0;
  309. struct input_dev *dev;
  310. /* allocate lightsensor input_device */
  311. dev = input_allocate_device();
  312. if (!dev)
  313. return -ENOMEM;
  314. dev->name = MODULE_NAME;
  315. dev->id.bustype = BUS_I2C;
  316. input_set_capability(dev, EV_REL, REL_RED);
  317. input_set_capability(dev, EV_REL, REL_GREEN);
  318. input_set_capability(dev, EV_REL, REL_BLUE);
  319. input_set_capability(dev, EV_REL, REL_WHITE);
  320. input_set_capability(dev, EV_REL, REL_X);
  321. input_set_capability(dev, EV_REL, REL_Y);
  322. input_set_drvdata(dev, data);
  323. ret = input_register_device(dev);
  324. if (ret < 0) {
  325. input_free_device(data->input);
  326. return ret;
  327. }
  328. ret = sensors_create_symlink(&dev->dev.kobj, dev->name);
  329. if (ret < 0) {
  330. input_unregister_device(dev);
  331. return ret;
  332. }
  333. ret = sysfs_create_group(&dev->dev.kobj, &light_attribute_group);
  334. if (ret < 0) {
  335. sensors_remove_symlink(&data->input->dev.kobj,
  336. data->input->name);
  337. input_unregister_device(dev);
  338. return ret;
  339. }
  340. data->input = dev;
  341. return 0;
  342. }
  343. static int cm3323_probe(struct i2c_client *client,
  344. const struct i2c_device_id *id)
  345. {
  346. int ret = -ENODEV;
  347. struct cm3323_p *data = NULL;
  348. pr_info("[SENSOR]: %s - Probe Start!\n", __func__);
  349. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  350. pr_err("[SENSOR]: %s - i2c_check_functionality error\n",
  351. __func__);
  352. goto exit;
  353. }
  354. data = kzalloc(sizeof(struct cm3323_p), GFP_KERNEL);
  355. if (data == NULL) {
  356. pr_err("[SENSOR]: %s - kzalloc error\n", __func__);
  357. ret = -ENOMEM;
  358. goto exit_kzalloc;
  359. }
  360. data->i2c_client = client;
  361. i2c_set_clientdata(client, data);
  362. #ifdef CONFIG_SEC_RUBENS_PROJECT
  363. sensor_power_on_vdd(data,1);
  364. #endif
  365. /* Check if the device is there or not. */
  366. ret = cm3323_setup_reg(data);
  367. if (ret < 0) {
  368. pr_err("[SENSOR]: %s - could not setup regs\n", __func__);
  369. goto exit_setup_reg;
  370. }
  371. /* input device init */
  372. ret = cm3323_input_init(data);
  373. if (ret < 0)
  374. goto exit_input_init;
  375. atomic_set(&data->delay, CM3323_DEFAULT_DELAY);
  376. data->time_count = 0;
  377. INIT_DELAYED_WORK(&data->work, cm3323_work_func_light);
  378. /* set sysfs for light sensor */
  379. sensors_register(data->light_dev, data, sensor_attrs, MODULE_NAME);
  380. pr_info("[SENSOR]: %s - Probe done!\n", __func__);
  381. return 0;
  382. exit_input_init:
  383. exit_setup_reg:
  384. kfree(data);
  385. #ifdef CONFIG_SEC_RUBENS_PROJECT
  386. sensor_power_on_vdd(data,0);
  387. #endif
  388. exit_kzalloc:
  389. exit:
  390. pr_err("[SENSOR]: %s - Probe fail!\n", __func__);
  391. return ret;
  392. }
  393. static void cm3323_shutdown(struct i2c_client *client)
  394. {
  395. struct cm3323_p *data = i2c_get_clientdata(client);
  396. pr_info("[SENSOR]: %s\n", __func__);
  397. if (data->power_state & LIGHT_ENABLED)
  398. cm3323_light_disable(data);
  399. }
  400. static int __devexit cm3323_remove(struct i2c_client *client)
  401. {
  402. struct cm3323_p *data = i2c_get_clientdata(client);
  403. /* device off */
  404. if (data->power_state & LIGHT_ENABLED)
  405. cm3323_light_disable(data);
  406. /* sysfs destroy */
  407. sensors_unregister(data->light_dev, sensor_attrs);
  408. sensors_remove_symlink(&data->input->dev.kobj, data->input->name);
  409. /* input device destroy */
  410. sysfs_remove_group(&data->input->dev.kobj, &light_attribute_group);
  411. input_unregister_device(data->input);
  412. kfree(data);
  413. return 0;
  414. }
  415. static int cm3323_suspend(struct device *dev)
  416. {
  417. struct cm3323_p *data = dev_get_drvdata(dev);
  418. if (data->power_state & LIGHT_ENABLED) {
  419. pr_info("[SENSOR]: %s\n", __func__);
  420. cm3323_light_disable(data);
  421. }
  422. return 0;
  423. }
  424. static int cm3323_resume(struct device *dev)
  425. {
  426. struct cm3323_p *data = dev_get_drvdata(dev);
  427. if (data->power_state & LIGHT_ENABLED) {
  428. pr_info("[SENSOR]: %s\n", __func__);
  429. cm3323_light_enable(data);
  430. }
  431. return 0;
  432. }
  433. static struct of_device_id cm3323_match_table[] = {
  434. { .compatible = "cm3323-i2c",},
  435. {},
  436. };
  437. static const struct i2c_device_id cm3323_device_id[] = {
  438. { "cm3323_match_table", 0 },
  439. { }
  440. };
  441. static const struct dev_pm_ops cm3323_pm_ops = {
  442. .suspend = cm3323_suspend,
  443. .resume = cm3323_resume
  444. };
  445. static struct i2c_driver cm3323_i2c_driver = {
  446. .driver = {
  447. .name = MODEL_NAME,
  448. .owner = THIS_MODULE,
  449. .of_match_table = cm3323_match_table,
  450. .pm = &cm3323_pm_ops
  451. },
  452. .probe = cm3323_probe,
  453. .shutdown = cm3323_shutdown,
  454. .remove = __devexit_p(cm3323_remove),
  455. .id_table = cm3323_device_id,
  456. };
  457. static int __init cm3323_init(void)
  458. {
  459. return i2c_add_driver(&cm3323_i2c_driver);
  460. }
  461. static void __exit cm3323_exit(void)
  462. {
  463. i2c_del_driver(&cm3323_i2c_driver);
  464. }
  465. module_init(cm3323_init);
  466. module_exit(cm3323_exit);
  467. MODULE_DESCRIPTION("RGB Sensor device driver for cm3323");
  468. MODULE_AUTHOR("Samsung Electronics");
  469. MODULE_LICENSE("GPL");