local_rled_chip.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. /*
  2. * controller's IR driver for the nordic52832 SoCs
  3. *
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. */
  10. #include <linux/string.h>
  11. #include <linux/wait.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/gpio.h>
  14. #include <linux/pinctrl/consumer.h>
  15. #include <linux/of.h>
  16. #include <linux/of_address.h>
  17. #include <linux/of_device.h>
  18. #include <linux/of_gpio.h>
  19. #include <linux/of_irq.h>
  20. #include <asm-generic/gpio.h>
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/init.h>
  24. #include <linux/device.h>
  25. #include <linux/kdev_t.h>
  26. #include <linux/types.h>
  27. #include <linux/bitops.h>
  28. #include <linux/errno.h>
  29. #include <linux/dma-buf.h>
  30. #include <linux/of.h>
  31. #include <linux/of_gpio.h>
  32. #include <linux/of_device.h>
  33. #include <linux/kernel.h>
  34. #include <linux/module.h>
  35. #include <linux/miscdevice.h>
  36. #include <linux/fs.h>
  37. #include <linux/moduleparam.h>
  38. #include <linux/slab.h>
  39. #include <linux/ioctl.h>
  40. #include <linux/cdev.h>
  41. #include <linux/delay.h>
  42. #include <linux/irqdomain.h>
  43. #include <linux/interrupt.h>
  44. #include <linux/irq.h>
  45. #include <linux/jiffies.h>
  46. #include <linux/timekeeping.h>
  47. #include <linux/kthread.h>
  48. #include <linux/delay.h>
  49. #include <linux/slab.h>
  50. #include <linux/gpio.h>
  51. #include <linux/of_gpio.h>
  52. #include <linux/spi/spi.h>
  53. #include <linux/of_irq.h>
  54. #include <linux/regulator/consumer.h>
  55. #include <linux/kernel.h>
  56. #include <linux/string.h>
  57. #include <linux/hrtimer.h>
  58. #include <linux/ktime.h>
  59. struct rled_chip_io {
  60. int torch_en;
  61. int flash_en;
  62. int charge_en;
  63. int flag_pin; /*rled chip flag pin from chip to cpu,indicate chip state*/
  64. int irq;
  65. };
  66. struct local_rled_chip {
  67. struct mutex change_lock;/*rled state chagen lock*/
  68. struct pinctrl *pinctrl;
  69. struct pinctrl_state *active;
  70. struct pinctrl_state *suspend;
  71. int rled_trig_gpio;
  72. int irq; /*from the camera sensor trig the irq*/
  73. struct rled_chip_io chip1;
  74. struct rled_chip_io chip2;
  75. struct hrtimer hr_timer;
  76. ktime_t ktime;
  77. int grled_chipenabled;
  78. int grled_swflag;/*Every other frame the trig led will use ,not all frame */
  79. };
  80. static enum hrtimer_restart timercallback( struct hrtimer *timer )
  81. {
  82. struct local_rled_chip *rled_data;
  83. rled_data = container_of(timer, struct local_rled_chip,hr_timer);
  84. gpio_set_value(rled_data->chip1.flash_en, 0);
  85. gpio_set_value(rled_data->chip2.flash_en, 0);
  86. //enable_irq(rled_data->irq);
  87. //pr_err("local_rled timercallback\n");
  88. return HRTIMER_NORESTART;
  89. }
  90. static irqreturn_t rled_irq_handler(int irq, void *dev_id)
  91. {
  92. int val=0;
  93. struct local_rled_chip *rled_data=(struct local_rled_chip *)dev_id;
  94. if(rled_data->grled_chipenabled==0)
  95. {
  96. gpio_set_value(rled_data->chip1.flash_en,0);
  97. gpio_set_value(rled_data->chip2.flash_en,0);
  98. return IRQ_HANDLED;
  99. }
  100. val=gpio_get_value(rled_data->rled_trig_gpio);
  101. if(val==1)
  102. {
  103. if(rled_data->grled_swflag)
  104. {
  105. gpio_set_value(rled_data->chip1.flash_en,1);
  106. gpio_set_value(rled_data->chip2.flash_en,1);
  107. }
  108. //disable_irq_nosync(rled_data->irq);
  109. //hrtimer_start( &rled_data->hr_timer, rled_data->ktime, HRTIMER_MODE_REL );
  110. }
  111. else
  112. {
  113. gpio_set_value(rled_data->chip1.flash_en,0);
  114. gpio_set_value(rled_data->chip2.flash_en,0);
  115. }
  116. //pr_err("rled_irq_handler val=%d\n",val);
  117. return IRQ_HANDLED;
  118. }
  119. /*just test code for the rled chip */
  120. static int grled_enabled=0;
  121. static int grled_mode=0;// 0 flash 1,torch ,0x80 charge enable
  122. static struct local_rled_chip *gprled_data=NULL;
  123. static ssize_t rledtest_show(struct device *dev,struct device_attribute *attr, char *buf)
  124. {
  125. return sprintf(buf, "mode[%d]enable[%d]\n",grled_mode,grled_enabled );
  126. }
  127. static ssize_t rledtest_store(struct device *dev,struct device_attribute *attr, const char *buf, size_t size)
  128. {
  129. if(gprled_data==NULL)return size;
  130. if(sscanf(buf,"%d:%d",&grled_mode,&grled_enabled)==2)
  131. {
  132. pr_err("local_rled mode =%d, rled = %d \n",grled_mode,grled_enabled);
  133. disable_irq_nosync(gprled_data->irq);//
  134. if((grled_mode&0x80)==0x80){ //TODO
  135. if(grled_enabled){
  136. gpio_set_value(gprled_data->chip1.charge_en,1);
  137. gpio_set_value(gprled_data->chip2.charge_en,1);
  138. }
  139. else
  140. {
  141. gpio_set_value(gprled_data->chip1.charge_en,0);
  142. gpio_set_value(gprled_data->chip2.charge_en,0);
  143. }
  144. }
  145. if((grled_mode&0x7f)==0){
  146. if(grled_enabled){
  147. gpio_set_value(gprled_data->chip1.flash_en,1);
  148. gpio_set_value(gprled_data->chip2.flash_en,1);
  149. }
  150. else
  151. {
  152. gpio_set_value(gprled_data->chip1.flash_en,0);
  153. gpio_set_value(gprled_data->chip2.flash_en,0);
  154. }
  155. }
  156. if((grled_mode&0x7f)==1){
  157. if(grled_enabled){
  158. gpio_set_value(gprled_data->chip1.torch_en,1);
  159. gpio_set_value(gprled_data->chip2.torch_en,1);
  160. }
  161. else
  162. {
  163. gpio_set_value(gprled_data->chip1.torch_en,0);
  164. gpio_set_value(gprled_data->chip2.torch_en,0);
  165. }
  166. }
  167. }
  168. return size;
  169. }
  170. /*just test code for the rled chip end*/
  171. static ssize_t rledchipenable_show(struct device *dev,struct device_attribute *attr, char *buf)
  172. {
  173. return sprintf(buf, "rledchipenable[%d]\n",gprled_data->grled_chipenabled );
  174. }
  175. static ssize_t rledchipenable_store(struct device *dev,struct device_attribute *attr, const char *buf, size_t size)
  176. {
  177. int enableflag=0;
  178. if(gprled_data==NULL)return size;
  179. if(sscanf(buf,"%d",&enableflag)==1)
  180. {
  181. pr_err("local_rled enableflag = %d \n",enableflag);
  182. mutex_lock(&gprled_data->change_lock);
  183. if(enableflag==1){
  184. if(gprled_data->grled_chipenabled==0){
  185. gprled_data->grled_chipenabled=1;
  186. enable_irq(gprled_data->irq);
  187. }
  188. }
  189. else{
  190. if(gprled_data->grled_chipenabled==1){
  191. gprled_data->grled_chipenabled=0;
  192. disable_irq_nosync(gprled_data->irq);
  193. }
  194. //if irq is not trig when led is on ,disable here .
  195. gpio_set_value(gprled_data->chip1.flash_en,0);
  196. gpio_set_value(gprled_data->chip2.flash_en,0);
  197. }
  198. mutex_unlock(&gprled_data->change_lock);
  199. }
  200. return size;
  201. }
  202. extern void external_ctl_gpio(u8 mask);
  203. static ssize_t grledswflag_show(struct device *dev,struct device_attribute *attr, char *buf)
  204. {
  205. return sprintf(buf, "rledchipenable[%d]\n",gprled_data->grled_swflag );
  206. }
  207. /*used to ctl V02A joystick */
  208. static ssize_t grledswflag_store(struct device *dev,struct device_attribute *attr, const char *buf, size_t size)
  209. {
  210. int swflag=0;
  211. if(gprled_data==NULL)return size;
  212. if(sscanf(buf,"%d",&swflag)==1)
  213. {
  214. if(swflag){
  215. external_ctl_gpio((u8)swflag);
  216. gprled_data->grled_swflag=1;
  217. }
  218. else{
  219. external_ctl_gpio(0);
  220. gprled_data->grled_swflag=0;
  221. }
  222. // pr_err("local_rled grledswflag = %d \n",gprled_data->grled_swflag);
  223. }
  224. return size;
  225. }
  226. static DEVICE_ATTR(rledtest, S_IRUGO|S_IWUSR|S_IWGRP, rledtest_show, rledtest_store);
  227. static DEVICE_ATTR(rledchipenable, S_IRUGO|S_IWUSR|S_IWGRP, rledchipenable_show, rledchipenable_store);
  228. static DEVICE_ATTR(grledswflag, S_IRUGO|S_IWUSR|S_IWGRP, grledswflag_show, grledswflag_store);
  229. static int rled_local_probe(struct platform_device *pdev)
  230. {
  231. struct device *dev = &pdev->dev;
  232. struct device_node *of_node = dev->of_node;
  233. int rc = 0;
  234. struct local_rled_chip *rled_data;
  235. if(of_node==NULL){
  236. pr_err("failed to check of_node \n");
  237. return -ENOMEM;
  238. }
  239. rled_data = kzalloc(sizeof(*rled_data), GFP_KERNEL);
  240. if (!rled_data) {
  241. return -ENOMEM;
  242. }
  243. rled_data->pinctrl= devm_pinctrl_get(dev);
  244. if (IS_ERR_OR_NULL(rled_data->pinctrl)) {
  245. rc = PTR_ERR(rled_data->pinctrl);
  246. pr_err("failed pinctrl, rc=%d\n", rc);
  247. goto data_free;
  248. }
  249. rled_data->active = pinctrl_lookup_state(rled_data->pinctrl, "pin_default");
  250. if (IS_ERR_OR_NULL(rled_data->active)) {
  251. rc = PTR_ERR(rled_data->active);
  252. pr_err("failed pinctrl active state, rc=%d\n", rc);
  253. goto data_free;
  254. }
  255. rled_data->suspend =pinctrl_lookup_state(rled_data->pinctrl, "pin_sleep");
  256. if (IS_ERR_OR_NULL(rled_data->suspend)) {
  257. rc = PTR_ERR(rled_data->suspend);
  258. pr_err("failed pinctrl suspend state, rc=%d\n", rc);
  259. goto data_free;
  260. }
  261. pr_err("rled_pinctrl t ok \n");
  262. rled_data->rled_trig_gpio= of_get_named_gpio(of_node,"yc,irq-gpio", 0);
  263. if (!gpio_is_valid(rled_data->rled_trig_gpio)) {
  264. pr_err("failed get rled_trig_gpio gpio, rc=%d\n", rc);
  265. rc = -EINVAL;
  266. goto data_free;
  267. }
  268. // 1
  269. rled_data->chip1.flag_pin= of_get_named_gpio(of_node,"yc,flag1_gpio", 0);
  270. if (!gpio_is_valid(rled_data->chip1.flag_pin)) {
  271. pr_err("failed get flag1_gpio gpio, rc=%d\n", rc);
  272. rc = -EINVAL;
  273. goto data_free;
  274. }
  275. rled_data->chip1.charge_en= of_get_named_gpio(of_node,"yc,chgen1_gpio", 0);
  276. if (!gpio_is_valid(rled_data->chip1.charge_en)) {
  277. pr_err("failed get chgen1_gpio gpio, rc=%d\n", rc);
  278. rc = -EINVAL;
  279. goto data_free;
  280. }
  281. rled_data->chip1.flash_en= of_get_named_gpio(of_node,"yc,flash1_gpio", 0);
  282. if (!gpio_is_valid(rled_data->chip1.flash_en)) {
  283. pr_err("failed get flash1_gpio gpio, rc=%d\n", rc);
  284. rc = -EINVAL;
  285. goto data_free;
  286. }
  287. rled_data->chip1.torch_en= of_get_named_gpio(of_node,"yc,torch1_gpio", 0);
  288. if (!gpio_is_valid(rled_data->chip1.torch_en)) {
  289. pr_err("failed get torch1_gpio gpio, rc=%d\n", rc);
  290. rc = -EINVAL;
  291. goto data_free;
  292. }
  293. // 2
  294. rled_data->chip2.flag_pin= of_get_named_gpio(of_node,"yc,flag2_gpio", 0);
  295. if (!gpio_is_valid(rled_data->chip2.flag_pin)) {
  296. pr_err("failed get flag2_gpio gpio, rc=%d\n", rc);
  297. rc = -EINVAL;
  298. goto data_free;
  299. }
  300. rled_data->chip2.charge_en= of_get_named_gpio(of_node,"yc,chgen2_gpio", 0);
  301. if (!gpio_is_valid(rled_data->chip2.charge_en)) {
  302. pr_err("failed get chgen2_gpio gpio, rc=%d\n", rc);
  303. rc = -EINVAL;
  304. goto data_free;
  305. }
  306. rled_data->chip2.flash_en= of_get_named_gpio(of_node,"yc,flash2_gpio", 0);
  307. if (!gpio_is_valid(rled_data->chip2.flash_en)) {
  308. pr_err("failed get flash2_gpio gpio, rc=%d\n", rc);
  309. rc = -EINVAL;
  310. goto data_free;
  311. }
  312. rled_data->chip2.torch_en= of_get_named_gpio(of_node,"yc,torch2_gpio", 0);
  313. if (!gpio_is_valid(rled_data->chip2.torch_en)) {
  314. pr_err("failed get torch2_gpio gpio, rc=%d\n", rc);
  315. rc = -EINVAL;
  316. goto data_free;
  317. }
  318. pr_err("rled_get_gpio ok \n");
  319. //----------------------------------------------------------
  320. if (gpio_is_valid(rled_data->rled_trig_gpio)) {
  321. pr_err("request for rled_trig_gpio =%d ", rled_data->rled_trig_gpio);
  322. rc = devm_gpio_request(dev,rled_data->rled_trig_gpio, "rled_trig_gpio");
  323. if (rc) {
  324. pr_err("request for rled_trig_gpio failed, rc=%d\n", rc);
  325. goto data_free;
  326. }
  327. }
  328. if (gpio_is_valid(rled_data->chip1.flag_pin)) {
  329. rc = devm_gpio_request(dev,rled_data->chip1.flag_pin, "rled1_flag_pin");
  330. if (rc) {
  331. pr_err("request for rled1_flag_pin failed, rc=%d\n", rc);
  332. goto data_free;
  333. }
  334. }
  335. if (gpio_is_valid(rled_data->chip1.charge_en)) {
  336. rc = devm_gpio_request(dev,rled_data->chip1.charge_en, "rled1_charge_en");
  337. if (rc) {
  338. pr_err("request for rled1_charge_en failed, rc=%d\n", rc);
  339. goto data_free;
  340. }
  341. }
  342. if (gpio_is_valid(rled_data->chip1.flash_en)) {
  343. rc = devm_gpio_request(dev,rled_data->chip1.flash_en, "rled1_flash_en");
  344. if (rc) {
  345. pr_err("request for rled1_flash_en failed, rc=%d\n", rc);
  346. goto data_free;
  347. }
  348. }
  349. if (gpio_is_valid(rled_data->chip1.torch_en)) {
  350. rc = devm_gpio_request(dev,rled_data->chip1.torch_en, "rled1_torch_en");
  351. if (rc) {
  352. pr_err("request for rled1_torch_en failed, rc=%d\n", rc);
  353. goto data_free;
  354. }
  355. }
  356. //
  357. if (gpio_is_valid(rled_data->chip2.flag_pin)) {
  358. rc = devm_gpio_request(dev,rled_data->chip2.flag_pin, "rled2_flag_pin");
  359. if (rc) {
  360. pr_err("request for rled2_flag_pin failed, rc=%d\n", rc);
  361. goto data_free;
  362. }
  363. }
  364. if (gpio_is_valid(rled_data->chip2.charge_en)) {
  365. rc = devm_gpio_request(dev,rled_data->chip2.charge_en, "rled2_charge_en");
  366. if (rc) {
  367. pr_err("request for rled2_charge_en failed, rc=%d\n", rc);
  368. goto data_free;
  369. }
  370. }
  371. if (gpio_is_valid(rled_data->chip2.flash_en)) {
  372. rc = devm_gpio_request(dev,rled_data->chip2.flash_en, "rled2_flash_en");
  373. if (rc) {
  374. pr_err("request for rled2_flash_en failed, rc=%d\n", rc);
  375. goto data_free;
  376. }
  377. }
  378. if (gpio_is_valid(rled_data->chip2.torch_en)) {
  379. rc = devm_gpio_request(dev,rled_data->chip2.torch_en, "rled2_torch_en");
  380. if (rc) {
  381. pr_err("request for rled2_torch_en failed, rc=%d\n", rc);
  382. goto data_free;
  383. }
  384. }
  385. pr_err("rled_gpio_request ok \n");
  386. //------------
  387. rc=pinctrl_select_state(rled_data->pinctrl ,rled_data->active);
  388. if (rc)
  389. pr_err("rled failed to set pin state, rc=%d\n",rc);
  390. gpio_direction_output(rled_data->chip1.charge_en, 0);
  391. gpio_direction_output(rled_data->chip1.flash_en, 0);
  392. gpio_direction_output(rled_data->chip1.torch_en, 0);
  393. gpio_direction_input(rled_data->chip1.flag_pin);
  394. gpio_direction_output(rled_data->chip2.charge_en, 0);
  395. gpio_direction_output(rled_data->chip2.flash_en, 0);
  396. gpio_direction_output(rled_data->chip2.torch_en, 0);
  397. gpio_direction_input(rled_data->chip2.flag_pin);
  398. gpio_direction_input(rled_data->rled_trig_gpio);
  399. rled_data->irq = gpio_to_irq(rled_data->rled_trig_gpio);
  400. if (rled_data->irq < 0) {
  401. rled_data->irq=-1;
  402. pr_err(" rled_irq err\n");
  403. }
  404. else{
  405. rled_data->grled_chipenabled=1;
  406. rled_data->grled_swflag=1;
  407. rc = devm_request_irq(dev,rled_data->irq, rled_irq_handler,IRQF_TRIGGER_RISING|IRQF_TRIGGER_FALLING , "rled", rled_data);//IRQF_TRIGGER_FALLING
  408. //disable_irq(rled_data->irq);
  409. if(rc<0)
  410. pr_err("rled request_irq err =%d \n",rled_data->irq);
  411. pr_err(" yc request_irq =%d\n",rled_data->irq);
  412. rled_data->ktime = ktime_set(0, 5000000);
  413. hrtimer_init( &rled_data->hr_timer, CLOCK_BOOTTIME, HRTIMER_MODE_REL);
  414. rled_data->hr_timer.function = timercallback;
  415. gpio_direction_output(rled_data->chip2.charge_en, 1);
  416. gpio_direction_output(rled_data->chip1.charge_en, 1);
  417. }
  418. device_create_file(dev, &dev_attr_rledtest);
  419. device_create_file(dev, &dev_attr_rledchipenable);
  420. device_create_file(dev, &dev_attr_grledswflag);
  421. gprled_data=rled_data;
  422. mutex_init(&rled_data->change_lock);
  423. return rc;
  424. data_free:
  425. kfree(rled_data);
  426. return rc;
  427. }
  428. static const struct of_device_id rled_test_match_table[] = {
  429. { .compatible = "yc,rled_test",
  430. },
  431. {}
  432. };
  433. static struct platform_driver local_rled_driver = {
  434. .driver = {
  435. .name = "yc,rled_test",
  436. .of_match_table = rled_test_match_table,
  437. },
  438. .probe = rled_local_probe,
  439. };
  440. static int __init rled_local_init(void)
  441. {
  442. return platform_driver_register(&local_rled_driver);
  443. }
  444. static void __exit rled_local_exit(void)
  445. {
  446. return platform_driver_unregister(&local_rled_driver);
  447. }
  448. module_init(rled_local_init);
  449. module_exit(rled_local_exit);
  450. MODULE_DESCRIPTION("nordic52832 rled driver");
  451. MODULE_LICENSE("GPL v2");