bgm.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/moduleparam.h>
  4. #include <linux/platform_device.h>
  5. #include <linux/input.h>
  6. #include <linux/delay.h>
  7. #include <linux/mutex.h>
  8. #include <linux/slab.h>
  9. #include <linux/uaccess.h>
  10. #include <linux/miscdevice.h>
  11. #include <linux/wakelock.h>
  12. #include <linux/fs.h>
  13. #include <linux/ioctl.h>
  14. #include <linux/gpio.h>
  15. #include <linux/of_gpio.h>
  16. #include <linux/sensors_core.h>
  17. #define VENDOR "SAMSUNG"
  18. #define CHIP_ID "COMMON"
  19. #define EVENT_TYPE_STATUS REL_HWHEEL
  20. #define EVENT_TYPE_RESULT REL_DIAL
  21. struct bgm_data {
  22. struct platform_device *pdev;
  23. struct miscdevice bgm_device;
  24. struct wake_lock bgm_wake_lock;
  25. struct work_struct strip_work;
  26. struct workqueue_struct *report_queue;
  27. struct workqueue_struct *indication_queue;
  28. struct delayed_work report_work;
  29. struct delayed_work indication_work;
  30. struct input_dev *input;
  31. struct device *bgm_dev;
  32. u8 enabled;
  33. int strip_irq;
  34. int notify_cmd;
  35. int state_cmd;
  36. u32 indication;
  37. u32 strip_status;
  38. int bgm_state;
  39. u32 ecg_enable;
  40. int check_loop;
  41. };
  42. static void report_state(struct bgm_data *data)
  43. {
  44. input_report_rel(data->input, EVENT_TYPE_STATUS,
  45. data->bgm_state + 1);
  46. input_sync(data->input);
  47. }
  48. static void start_check(struct bgm_data *data)
  49. {
  50. data->check_loop = 1;
  51. queue_delayed_work(data->indication_queue,
  52. &data->indication_work, HZ/2);
  53. }
  54. static void stop_check(struct bgm_data *data)
  55. {
  56. data->check_loop = 0;
  57. //cancel_delayed_work(&data->indication_work);
  58. }
  59. static void indication_func(struct work_struct *work)
  60. {
  61. struct bgm_data *data = container_of(work,
  62. struct bgm_data, indication_work.work);
  63. int strip_value, indi_value;
  64. indi_value = gpio_get_value(data->indication);
  65. strip_value = gpio_get_value(data->strip_status);
  66. pr_info("[BGM] %s : strip=%d, indication=%d\n", __func__, strip_value, indi_value);
  67. if (indi_value == 1 && data->bgm_state == 0 && strip_value == 0) {
  68. pr_info("[BGM] %s : Start-up check OK\n", __func__);
  69. data->bgm_state = 1;
  70. report_state(data);
  71. } else if (indi_value == 0 && data->bgm_state == 1) {
  72. pr_info("[BGM] %s : Waiting sample\n", __func__);
  73. data->bgm_state = 2;
  74. report_state(data);
  75. } else if (indi_value == 1 && data->bgm_state == 2) {
  76. pr_info("[BGM] %s : Sample applied\n", __func__);
  77. data->bgm_state = 3;
  78. report_state(data);
  79. } else if (indi_value == 1 && data->bgm_state == 3
  80. && data->notify_cmd == 1) {
  81. pr_info("[BGM] %s : Send sleep\n", __func__);
  82. data->bgm_state = 4;
  83. report_state(data);
  84. }
  85. if (data->check_loop)
  86. queue_delayed_work(data->indication_queue,
  87. &data->indication_work, HZ/2);
  88. }
  89. static void report_func(struct work_struct *work)
  90. {
  91. struct bgm_data *data = container_of(work,
  92. struct bgm_data, report_work.work);
  93. pr_info("[BGM] %s : %d\n", __func__, data->notify_cmd + 1);
  94. input_report_rel(data->input, EVENT_TYPE_STATUS,
  95. data->notify_cmd + 1);
  96. input_sync(data->input);
  97. }
  98. static irqreturn_t bgm_strip_irq_handler(int irq, void *dev_id)
  99. {
  100. struct bgm_data *data = dev_id;
  101. wake_lock_timeout(&data->bgm_wake_lock, 3 * HZ);
  102. //schedule_work(&data->strip_work);
  103. pr_info("[BGM] %s : IRQ_HANDLED. %d\n", __func__,
  104. gpio_get_value(data->strip_status));
  105. return IRQ_HANDLED;
  106. }
  107. static void strip_func(struct work_struct *work)
  108. {
  109. struct bgm_data *data = container_of((struct work_struct *)work,
  110. struct bgm_data, strip_work);
  111. int ret;
  112. ret = gpio_get_value(data->strip_status);
  113. pr_info("[BGM] %s : strip gpio value is %d\n", __func__, ret);
  114. }
  115. static int bgm_parse_dt(struct device *dev, struct bgm_data *data)
  116. {
  117. struct device_node *np = dev->of_node;
  118. enum of_gpio_flags flags;
  119. int errorno = 0;
  120. int irq;
  121. int rc;
  122. pr_info("[BGM] %s : bgm,indication\n", __func__);
  123. data->indication = of_get_named_gpio_flags(np, "bgm,indication",
  124. 0, &flags);
  125. if (data->indication < 0) {
  126. errorno = data->indication;
  127. pr_err("[BGM] %s : bgm,indication error\n", __func__);
  128. } else {
  129. errorno = gpio_request(data->indication, "BGMINDI");
  130. if (errorno) {
  131. pr_err("[BGM] %s : failed to request indication PIN\n",
  132. __func__);
  133. } else {
  134. errorno = gpio_direction_input(data->indication);
  135. if (errorno) {
  136. pr_err("[BGM] %s : failed to set indication PIN as output\n",
  137. __func__);
  138. }
  139. }
  140. }
  141. pr_info("[BGM] %s : bgm,strip_status\n", __func__);
  142. data->strip_status = of_get_named_gpio_flags(np, "bgm,strip_status",
  143. 0, &flags);
  144. if (data->strip_status < 0) {
  145. errorno = data->strip_status;
  146. pr_err("[BGM] %s : bgm,strip_status\n", __func__);
  147. } else {
  148. errorno = gpio_request(data->strip_status, "BGMSTRIP");
  149. if (errorno) {
  150. pr_err("[BGM] %s : failed to request strip_status PIN\n",
  151. __func__);
  152. } else {
  153. errorno = gpio_direction_input(data->strip_status);
  154. if (errorno) {
  155. pr_err("[BGM] %s : failed to set strip_status PIN as output\n",
  156. __func__);
  157. }
  158. }
  159. }
  160. if (data->strip_status >= 0) {
  161. irq = gpio_to_irq(data->strip_status);
  162. rc = request_threaded_irq(irq, NULL,
  163. bgm_strip_irq_handler,
  164. IRQF_TRIGGER_FALLING |
  165. IRQF_TRIGGER_RISING |
  166. IRQF_ONESHOT,
  167. "bgm_ind_int",
  168. data);
  169. if (rc < 0) {
  170. pr_err("[BGM] %s : request_irq(%d) failed for gpio %d (%d)\n",
  171. __func__, irq,
  172. data->strip_status, rc);
  173. //goto dt_exit;
  174. }
  175. disable_irq(irq);
  176. data->strip_irq = irq;
  177. }
  178. #if 1
  179. pr_info("[BGM] %s : bgm,ecg_enable\n", __func__);
  180. data->ecg_enable = of_get_named_gpio_flags(np, "bgm,ecg_enable",
  181. 0, &flags);
  182. if (data->ecg_enable < 0) {
  183. errorno = data->ecg_enable;
  184. pr_err("[BGM] %s: mlx90615,ecg_enable\n", __func__);
  185. } else {
  186. errorno = gpio_request(data->ecg_enable, "BGM_ECG_ENABLE");
  187. if (errorno) {
  188. pr_err("[BGM] %s: failed to request ecg_enable PIN \n",
  189. __func__);
  190. } else {
  191. errorno = gpio_direction_output(data->ecg_enable, 1);
  192. if (errorno) {
  193. pr_err("[BGM] %s: failed to set ecg_enable PIN as output\n",
  194. __func__);
  195. }
  196. }
  197. }
  198. #endif
  199. return 0;
  200. //dt_exit:
  201. //return errorno;
  202. }
  203. /*
  204. static ssize_t bgm_io_read(struct file *filp, char __user *buf,
  205. size_t count, loff_t *offset)
  206. {
  207. //struct bgm_data *data = filp->private_data;
  208. //char tmp[MAX_BUFFER_SIZE] = {0, };
  209. int ret = 0;
  210. pr_info("[BGM] %s :\n", __func__);
  211. //ret = wait_event_interruptible(pn547_dev->read_wq,
  212. return ret;
  213. }
  214. static ssize_t bgm_io_write(struct file *filp, const char __user *buf,
  215. size_t count, loff_t *offset)
  216. {
  217. int ret = 0;
  218. struct bgm_data *data =filp->private_data;
  219. pr_info("[BGM] %s :\n", __func__);
  220. return ret;
  221. }
  222. static long bgm_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  223. {
  224. // void __user *argp = (void __user *)arg;
  225. // struct bgm_data *data = container_of(filp->private_data,
  226. // struct bgm_data, bgm_device);
  227. switch (cmd) {
  228. default:
  229. return -ENOTTY;
  230. }
  231. return 0;
  232. }
  233. */
  234. static int bgm_io_open(struct inode *inode, struct file *filp)
  235. {
  236. pr_info("[BGM] %s :\n", __func__);
  237. return 0;
  238. }
  239. static int bgm_io_release(struct inode *inode, struct file *filp)
  240. {
  241. pr_info("[BGM] %s :\n", __func__);
  242. return 0;
  243. }
  244. static const struct file_operations bgm_fops = {
  245. .owner = THIS_MODULE,
  246. .open = bgm_io_open,
  247. .release = bgm_io_release,
  248. //.unlocked_ioctl = bgm_ioctl,
  249. .llseek = no_llseek,
  250. //.read = bgm_io_read,
  251. //.write = bgm_io_write,
  252. };
  253. static ssize_t bgm_enable_store(struct device *dev,
  254. struct device_attribute *attr,
  255. const char *buf, size_t size)
  256. {
  257. struct bgm_data *data = dev_get_drvdata(dev);
  258. int new_value;
  259. if (sysfs_streq(buf, "1"))
  260. new_value = true;
  261. else if (sysfs_streq(buf, "0"))
  262. new_value = false;
  263. else {
  264. pr_err("[BGM] %s: invalid value %d\n", __func__, *buf);
  265. return -EINVAL;
  266. }
  267. if (new_value && !data->enabled) {
  268. pr_err("[BGM] %s : bgm enable, polling start, sensor on\n", __func__);
  269. data->bgm_state = 0;
  270. data->notify_cmd = 0;
  271. data->enabled = 1;
  272. gpio_direction_output(data->ecg_enable, 0);
  273. msleep(300);
  274. gpio_direction_output(data->ecg_enable, 1);
  275. start_check(data);
  276. } else if (!new_value && data->enabled) {
  277. data->enabled = 0;
  278. pr_err("[BGM] %s : bgm enable, polling stop, sensor off\n", __func__);
  279. gpio_direction_output(data->ecg_enable, 1);
  280. stop_check(data);
  281. }
  282. return size;
  283. }
  284. static ssize_t bgm_notify_store(struct device *dev,
  285. struct device_attribute *attr,
  286. const char *buf, size_t size)
  287. {
  288. int err;
  289. long noti_cmd;
  290. struct bgm_data *data = dev_get_drvdata(dev);
  291. err = strict_strtol(buf, 10, &noti_cmd);
  292. pr_info("[BGM] %s : %s = 0x%02x\n", __func__, buf, (int)noti_cmd);
  293. if (err < 0)
  294. return -EINVAL;
  295. if (noti_cmd == 1)
  296. data->notify_cmd = 1;
  297. else { /* error or test state */
  298. input_report_rel(data->input, EVENT_TYPE_STATUS, noti_cmd + 1);
  299. input_sync(data->input);
  300. data->bgm_state = 0;
  301. data->notify_cmd = 0;
  302. }
  303. //queue_delayed_work(data->report_queue, &data->report_work, HZ*2);
  304. //TODO : input report
  305. return size;
  306. }
  307. static ssize_t bgm_notify_show(struct device *dev,
  308. struct device_attribute *attr, char *buf)
  309. {
  310. struct bgm_data *data = dev_get_drvdata(dev);
  311. return sprintf(buf, "%d\n", data->notify_cmd);
  312. }
  313. static ssize_t bgm_enable_show(struct device *dev,
  314. struct device_attribute *attr, char *buf)
  315. {
  316. struct bgm_data *data = dev_get_drvdata(dev);
  317. return sprintf(buf, "%d\n", data->enabled);
  318. }
  319. static struct device_attribute dev_attr_enable =
  320. __ATTR(enable, S_IRUGO | S_IWUSR | S_IWGRP,
  321. bgm_enable_show, bgm_enable_store);
  322. static struct attribute *bgm_sysfs_attrs[] = {
  323. &dev_attr_enable.attr,
  324. NULL
  325. };
  326. static struct attribute_group bgm_attribute_group = {
  327. .attrs = bgm_sysfs_attrs,
  328. };
  329. /* sysfs for vendor & name */
  330. static ssize_t bgm_vendor_show(struct device *dev,
  331. struct device_attribute *attr, char *buf)
  332. {
  333. return sprintf(buf, "%s\n", VENDOR);
  334. }
  335. static ssize_t bgm_name_show(struct device *dev,
  336. struct device_attribute *attr, char *buf)
  337. {
  338. return sprintf(buf, "%s\n", CHIP_ID);
  339. }
  340. static DEVICE_ATTR(vendor, 0644, bgm_vendor_show, NULL);
  341. static DEVICE_ATTR(name, 0644, bgm_name_show, NULL);
  342. static DEVICE_ATTR(notify, 0666, bgm_notify_show, bgm_notify_store);
  343. static struct device_attribute *bgm_sensor_attrs[] = {
  344. &dev_attr_vendor,
  345. &dev_attr_name,
  346. &dev_attr_notify,
  347. NULL,
  348. };
  349. static int bgm_probe(struct platform_device *dev)
  350. {
  351. int ret;
  352. struct bgm_data *data;
  353. pr_info("[BGM] %s : bgm_probe\n", __func__);
  354. data = kzalloc(sizeof(*data), GFP_KERNEL);
  355. if (data == NULL) {
  356. pr_err("%s, failed to alloc memory for module data\n",
  357. __func__);
  358. ret = -ENOMEM;
  359. goto exit;
  360. }
  361. data->pdev = platform_device_register_simple("bgm", -1, NULL, 0);
  362. if (IS_ERR(data->pdev)) {
  363. ret = PTR_ERR(data->pdev);
  364. goto out_driver;
  365. }
  366. if (dev->dev.of_node)
  367. pr_info("[BGM] %s : of node\n", __func__);
  368. else {
  369. pr_err("%s : no of node\n", __func__);
  370. goto out_device;
  371. }
  372. ret = bgm_parse_dt(&dev->dev, data);
  373. if (ret) {
  374. pr_err("%s : parse dt error\n", __func__);
  375. //goto err_parse_dt;
  376. }
  377. data->bgm_state = 0;
  378. data->bgm_device.minor = MISC_DYNAMIC_MINOR;
  379. data->bgm_device.name = "bgm";
  380. data->bgm_device.fops = &bgm_fops;
  381. ret = misc_register(&data->bgm_device);
  382. if (ret)
  383. goto exit_misc_device_register_failed;
  384. wake_lock_init(&data->bgm_wake_lock,
  385. WAKE_LOCK_SUSPEND, "bgm_wake_lock");
  386. INIT_WORK(&data->strip_work, strip_func);
  387. INIT_DELAYED_WORK(&data->report_work, report_func);
  388. INIT_DELAYED_WORK(&data->indication_work, indication_func);
  389. data->report_queue =
  390. create_singlethread_workqueue(dev_name(&data->pdev->dev));
  391. data->indication_queue =
  392. create_singlethread_workqueue(dev_name(&data->pdev->dev));
  393. data->input = input_allocate_device();
  394. if (!data->input) {
  395. pr_err("[BGM] %s : could not allocate mlx90615 input device\n", __func__);
  396. goto err_input_allocate_device;
  397. }
  398. data->input->name = "bgm_sensor";
  399. input_set_capability(data->input, EV_REL, EVENT_TYPE_STATUS);
  400. //input_set_capability(data->input, EV_REL, EVENT_TYPE_OBJECT_TEMP);
  401. input_set_drvdata(data->input, data);
  402. ret = input_register_device(data->input);
  403. if (ret < 0) {
  404. input_free_device(data->input);
  405. pr_err("[BGM] %s : could not register input device\n", __func__);
  406. //goto err_input_register_device;
  407. }
  408. ret = sensors_create_symlink(&data->input->dev.kobj,
  409. data->input->name);
  410. if (ret < 0) {
  411. input_unregister_device(data->input);
  412. goto err_sysfs_create_symlink;
  413. }
  414. ret = sysfs_create_group(&data->input->dev.kobj,
  415. &bgm_attribute_group);
  416. if (ret) {
  417. pr_err("[BGM] %s : could not create sysfs group\n", __func__);
  418. goto err_sysfs_create_group;
  419. }
  420. ret = sensors_register(data->bgm_dev,
  421. data, bgm_sensor_attrs, "bgm_sensor");
  422. if (ret) {
  423. pr_err("[BGM] %s : cound not register prox sensor device(%d).\n",
  424. __func__, ret);
  425. goto err_sysfs_create_symlink;
  426. }
  427. platform_set_drvdata(dev,data);
  428. pr_info("[BGM] %s : success.\n", __func__);
  429. return 0;
  430. err_input_allocate_device:
  431. err_sysfs_create_symlink:
  432. input_free_device(data->input);
  433. err_sysfs_create_group:
  434. input_unregister_device(data->input);
  435. exit_misc_device_register_failed:
  436. misc_deregister(&data->bgm_device);
  437. out_device:
  438. platform_device_unregister(data->pdev);
  439. out_driver:
  440. //mutex_destroy(&data->bgm_lock);
  441. kfree(data);
  442. exit:
  443. pr_err("[BGM] %s : failed!\n", __func__);
  444. return ret;
  445. }
  446. static int bgm_remove(struct platform_device *dev)
  447. {
  448. struct bgm_data *data = platform_get_drvdata(dev);
  449. misc_deregister(&data->bgm_device);
  450. platform_device_unregister(data->pdev);
  451. kfree(data);
  452. return 0;
  453. }
  454. static int bgm_suspend(struct device *dev)
  455. {
  456. struct bgm_data *data = dev_get_drvdata(dev);
  457. gpio_direction_output(data->ecg_enable, 0);
  458. return 0;
  459. }
  460. static int bgm_resume(struct device *dev)
  461. {
  462. struct bgm_data *data = dev_get_drvdata(dev);
  463. gpio_direction_output(data->ecg_enable, 1);
  464. cancel_delayed_work(&data->report_work);
  465. return 0;
  466. }
  467. #ifdef CONFIG_OF
  468. static struct of_device_id bgm_match_table[] = {
  469. {.compatible = "blood_glucose",},
  470. {},
  471. };
  472. #endif
  473. static const struct dev_pm_ops bgm_pm_ops = {
  474. .suspend = bgm_suspend,
  475. .resume = bgm_resume
  476. };
  477. static struct platform_driver bgm_driver = {
  478. .driver = {
  479. #ifndef CONFIG_HAS_EARLYSUSPEND
  480. .pm = &bgm_pm_ops,
  481. #endif
  482. .name = "blood_glucose",
  483. .owner = THIS_MODULE,
  484. .of_match_table = bgm_match_table,
  485. },
  486. .probe = bgm_probe,
  487. .remove = bgm_remove,
  488. };
  489. static int __init bgm_init(void)
  490. {
  491. return platform_driver_register(&bgm_driver);
  492. }
  493. static void __exit bgm_exit(void)
  494. {
  495. platform_driver_unregister(&bgm_driver);
  496. }
  497. module_init(bgm_init);
  498. module_exit(bgm_exit);
  499. MODULE_DESCRIPTION("BGM sensor control");
  500. MODULE_AUTHOR("Samsung Electronic");
  501. MODULE_LICENSE("GPL v2");