flash_led_driver.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. /****************************************************************************
  2. *
  3. * The MIT License (MIT)
  4. *
  5. * Copyright (c) 2020 VeriSilicon Holdings Co., Ltd.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. *
  25. *****************************************************************************
  26. *
  27. * The GPL License (GPL)
  28. *
  29. * Copyright (c) 2020 VeriSilicon Holdings Co., Ltd.
  30. *
  31. * This program is free software; you can redistribute it and/or
  32. * modify it under the terms of the GNU General Public License
  33. * as published by the Free Software Foundation; either version 2
  34. * of the License, or (at your option) any later version.
  35. *
  36. * This program is distributed in the hope that it will be useful,
  37. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  38. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  39. * GNU General Public License for more details.
  40. *
  41. * You should have received a copy of the GNU General Public License
  42. * along with this program;
  43. *
  44. *****************************************************************************
  45. *
  46. * Note: This software is released under dual MIT and GPL licenses. A
  47. * recipient may use this file under the terms of either the MIT license or
  48. * GPL License. If you wish to use only one license not the other, you can
  49. * indicate your decision by deleting one of the above license notices in your
  50. * version of this file.
  51. *
  52. *****************************************************************************/
  53. #include <asm/io.h>
  54. #include <linux/cdev.h>
  55. #include <linux/module.h>
  56. #include <linux/platform_device.h>
  57. #include <linux/delay.h>
  58. #include <linux/clk.h>
  59. #include <linux/io.h>
  60. #include <linux/mm.h>
  61. #include <linux/slab.h>
  62. #include <linux/proc_fs.h>
  63. #include <linux/debugfs.h>
  64. #include <linux/miscdevice.h>
  65. #include <linux/uaccess.h>
  66. #include <linux/regulator/consumer.h>
  67. #include <linux/time.h>
  68. #include <linux/timex.h>
  69. #include <linux/interrupt.h>
  70. #include <linux/delay.h>
  71. #include <linux/i2c.h>
  72. #include <linux/gpio.h>
  73. #include <linux/kernel.h>
  74. #include <linux/of.h>
  75. #include <linux/of_address.h>
  76. #include <linux/of_gpio.h>
  77. #include <linux/spinlock.h>
  78. #include <linux/dma-mapping.h>
  79. #include <linux/dma-buf.h>
  80. #include <linux/iio/consumer.h>
  81. #include "flash_led_driver.h"
  82. #include "flash_led_ioctl.h"
  83. #define FLASH_LED_NAME "flash_led"
  84. #define FLASH_LED_MAXCNT 10
  85. static unsigned int flash_led_major = 0;
  86. static unsigned int flash_led_minor = 0;
  87. struct class *flash_led_class;
  88. static unsigned int devise_register_index = 0;
  89. extern int get_ntc_temperature(int mv);
  90. static irqreturn_t touch_pin_isr(int irq, void *dev);
  91. static int flash_pin_init(struct flash_led_ctrl *dev);
  92. static int touch_pin_uinit(struct flash_led_ctrl *dev);
  93. int flash_led_switch(struct flash_led_ctrl *pflash_led_dev ,uint64_t frame_id)
  94. {
  95. struct flash_led_dev *floodlight = &pflash_led_dev->floodlight;
  96. struct flash_led_dev *projection = &pflash_led_dev->projection;
  97. bool floodlight_on =false;
  98. bool projection_on =false;
  99. switch(pflash_led_dev->switch_mode)
  100. {
  101. case PROJECTION_EVEN_FLOODLIGHT_ODD:
  102. if(frame_id%2)
  103. projection_on = true;
  104. else
  105. floodlight_on =true;
  106. break;
  107. case PROJECTION_ODD_FLOODLIGHT_EVEN:
  108. if(frame_id%2)
  109. floodlight_on = true;
  110. else
  111. projection_on =true;
  112. break;
  113. case PROJECTION_ALWAYS_ON:
  114. projection_on = true;
  115. break;
  116. case FLOODLIGHT_ALWAYS_ON:
  117. floodlight_on =true;
  118. break;
  119. case BOTH_OFF:
  120. break;
  121. default:
  122. pr_warn("invald switch mode:%d\n",pflash_led_dev->switch_mode);
  123. return -1;
  124. }
  125. if(projection->flash_led_func)
  126. {
  127. if(projection_on ==true && (pflash_led_dev->enable & PROJECTION_EN))
  128. projection->flash_led_func->enable_channel(projection, 3);
  129. else
  130. projection->flash_led_func->disable_channel(projection, 3);
  131. }
  132. if(floodlight->flash_led_func)
  133. {
  134. if(floodlight_on == true && (pflash_led_dev->enable & FLOODLIGHT_EN))
  135. floodlight->flash_led_func->enable_channel(floodlight, 3);
  136. else
  137. floodlight->flash_led_func->disable_channel(floodlight, 3);
  138. }
  139. return 0;
  140. }
  141. static int flash_led_open(struct inode * inode, struct file * file)
  142. {
  143. int ret = 0;
  144. int irq = 0;
  145. struct flash_led_driver_dev *pdriver_dev;
  146. struct flash_led_ctrl *pflash_led_dev;
  147. pdriver_dev = container_of(inode->i_cdev, struct flash_led_driver_dev, cdev);
  148. file->private_data = pdriver_dev;
  149. pflash_led_dev = (struct flash_led_ctrl*)pdriver_dev->private;
  150. struct flash_led_dev *floodlight = &pflash_led_dev->floodlight;
  151. struct flash_led_dev *projection = &pflash_led_dev->projection;
  152. irq = gpio_to_irq(pflash_led_dev->touch_pin);
  153. request_irq(irq, touch_pin_isr, IRQF_TRIGGER_FALLING,
  154. "flash led touch pin",
  155. pflash_led_dev);
  156. if (floodlight->flash_led_func != NULL) {
  157. ret = floodlight->flash_led_func->init(floodlight);
  158. if (ret != 0) {
  159. pr_err("%s, %d, floodlight init error\n", __func__, __LINE__);
  160. return ret;
  161. }
  162. }
  163. if (projection->flash_led_func != NULL) {
  164. ret = projection->flash_led_func->init(projection);
  165. if (ret != 0) {
  166. pr_err("%s, %d, projection init error\n", __func__, __LINE__);
  167. return ret;
  168. }
  169. }
  170. return 0;
  171. };
  172. static int flash_led_release(struct inode * inode, struct file * file)
  173. {
  174. int irq = -1;
  175. struct flash_led_driver_dev *pdriver_dev;
  176. pdriver_dev = container_of(inode->i_cdev, struct flash_led_driver_dev, cdev);
  177. struct flash_led_ctrl *pflash_led_dev = (struct flash_led_ctrl*)pdriver_dev->private;
  178. struct flash_led_dev *floodlight = &pflash_led_dev->floodlight;
  179. struct flash_led_dev *projection = &pflash_led_dev->projection;
  180. irq = gpio_to_irq(pflash_led_dev->touch_pin);
  181. free_irq(irq, pflash_led_dev);
  182. pflash_led_dev->enable = 0;
  183. if (projection->flash_led_func != NULL) {
  184. projection->flash_led_func->disable_channel(projection, 3);
  185. }
  186. if (floodlight->flash_led_func != NULL) {
  187. floodlight->flash_led_func->disable_channel(floodlight, 3);
  188. }
  189. return 0;
  190. };
  191. static long flash_led_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  192. {
  193. long ret = 0;
  194. struct flash_led_driver_dev *pdriver_dev;
  195. struct flash_led_ctrl *pflash_led_dev;
  196. pdriver_dev = file->private_data;
  197. if (pdriver_dev == NULL)
  198. {
  199. pr_err("%s:file private is null point error\n", __func__);
  200. return -ENOMEM;
  201. }
  202. pflash_led_dev = (struct flash_led_ctrl*)pdriver_dev->private;
  203. mutex_lock(&pdriver_dev->vvmutex);
  204. ret = flash_led_priv_ioctl(pflash_led_dev, cmd, (void __user *)arg);
  205. mutex_unlock(&pdriver_dev->vvmutex);
  206. return ret;
  207. };
  208. struct file_operations flash_led_fops = {
  209. .owner = THIS_MODULE,
  210. .open = flash_led_open,
  211. .release = flash_led_release,
  212. .unlocked_ioctl = flash_led_ioctl,
  213. };
  214. static int flash_led_of_parse(struct platform_device *pdev)
  215. {
  216. int ret = 0, regulator_num = 0;
  217. struct device_node *np = pdev->dev.of_node;
  218. struct flash_led_driver_dev *pdriver_dev = platform_get_drvdata(pdev);
  219. struct flash_led_ctrl *pflash_led_dev = pdriver_dev->private;
  220. struct flash_led_dev *floodlight = &pflash_led_dev->floodlight;
  221. struct flash_led_dev *projection = &pflash_led_dev->projection;
  222. ret = of_property_read_string(np, "flash_led_name", &pflash_led_dev->flash_led_name);
  223. if (ret < 0) {
  224. pr_err("%s:property flash_led_name not defined for %s\n", __func__, pdev->name);
  225. return ret;
  226. }
  227. // the flash led touch pin used touch the io interrupt
  228. pflash_led_dev->touch_pin = of_get_named_gpio(np, "flash_led_touch_pin", 0);
  229. if (pflash_led_dev->touch_pin >= 0) {
  230. ret = devm_gpio_request(&pdev->dev, pflash_led_dev->touch_pin,
  231. "flash_led_touch_pin");
  232. if (ret < 0) {
  233. pr_err("%s:flash_led_touch request failed\n", __func__);
  234. }
  235. } else {
  236. pr_err("flash_led_touch not defined for %s\n", pflash_led_dev->flash_led_name);
  237. }
  238. pflash_led_dev->floodlight_en_pin = of_get_named_gpio(np, "floodlight_en_pin", 0);
  239. if (pflash_led_dev->floodlight_en_pin >= 0) {
  240. ret = devm_gpio_request(&pdev->dev, pflash_led_dev->floodlight_en_pin,
  241. "floodlight_en_pin");
  242. if (ret < 0) {
  243. pr_err("%s:floodlight_en_pin request failed\n", __func__);
  244. }
  245. } else {
  246. pflash_led_dev->floodlight_en_pin = -1;
  247. pr_err("floodlight_en_pin not defined for %s\n", pflash_led_dev->flash_led_name);
  248. }
  249. pflash_led_dev->projection_en_pin = of_get_named_gpio(np, "projection_en_pin", 0);
  250. if (pflash_led_dev->projection_en_pin >= 0) {
  251. ret = devm_gpio_request(&pdev->dev, pflash_led_dev->projection_en_pin,
  252. "projection_en_pin");
  253. if (ret < 0) {
  254. pr_err("%s:projection_en_pin request failed\n", __func__);
  255. }
  256. } else {
  257. pflash_led_dev->projection_en_pin = -1;
  258. pr_err("projection_en_pin not defined for %s\n", pflash_led_dev->flash_led_name);
  259. }
  260. floodlight->regulators.num = of_property_count_strings(np, "floodlight_regulators");
  261. if (floodlight->regulators.num <= 0) {
  262. pr_info("%s:property floodlight_regulators not defined for %s\n", __func__, pdev->name);
  263. } else {
  264. pr_info("%s num_of_regulators %d\n", __func__, floodlight->regulators.num);
  265. ret = of_property_read_string_array(np, "floodlight_regulators",
  266. floodlight->regulators.name, floodlight->regulators.num);
  267. if (ret != floodlight->regulators.num) {
  268. pr_err("%s:fail to read property floodlight_regulators\n", __func__);
  269. return -1;
  270. };
  271. regulator_num = floodlight->regulators.num;
  272. while (regulator_num) {
  273. floodlight->regulators.supply[regulator_num - 1] = devm_regulator_get(&pdev->dev,
  274. floodlight->regulators.name[regulator_num - 1]);
  275. if (IS_ERR(floodlight->regulators.supply[regulator_num - 1])) {
  276. pr_err("%s:fail to devm_regulator_get %s\n", __func__, floodlight->regulators.name[regulator_num - 1]);
  277. return -1;
  278. }
  279. regulator_num--;
  280. }
  281. }
  282. projection->regulators.num = of_property_count_strings(np, "projection_regulators");
  283. if (projection->regulators.num <= 0) {
  284. pr_info("%s:property projection_regulators not defined for %s\n", __func__, pdev->name);
  285. } else {
  286. pr_info("%s num_of_regulators %d\n", __func__, projection->regulators.num);
  287. ret = of_property_read_string_array(np, "projection_regulators",
  288. projection->regulators.name, projection->regulators.num);
  289. if (ret != projection->regulators.num) {
  290. pr_err("%s:fail to read property projection_regulators\n", __func__);
  291. return -1;
  292. };
  293. regulator_num = projection->regulators.num;
  294. while (regulator_num) {
  295. projection->regulators.supply[regulator_num - 1] = devm_regulator_get(&pdev->dev,
  296. projection->regulators.name[regulator_num - 1]);
  297. if (IS_ERR(projection->regulators.supply[regulator_num - 1])) {
  298. pr_err("%s:fail to devm_regulator_get %s\n", __func__, projection->regulators.name[regulator_num - 1]);
  299. return -1;
  300. }
  301. regulator_num--;
  302. }
  303. }
  304. ret = of_property_read_u8(np, "floodlight_i2c_bus",
  305. &floodlight->i2c_bus);
  306. if (ret != 0) {
  307. pr_err("fail to read property floodlight_i2c_bus\n");
  308. floodlight->i2c_bus = UNDEFINED_IN_DTS;
  309. }
  310. ret = of_property_read_u8(np, "projection_i2c_bus",
  311. &projection->i2c_bus);
  312. if (ret != 0) {
  313. pr_err("fail to read property projection_i2c_bus\n");
  314. projection->i2c_bus = UNDEFINED_IN_DTS;
  315. }
  316. pflash_led_dev->projection_adc = iio_channel_get(&pdev->dev, "projection_adc");
  317. if (IS_ERR(pflash_led_dev->projection_adc)) {
  318. pr_err("not define projection_adc adc\n");
  319. }
  320. pflash_led_dev->floodlight_adc = iio_channel_get(&pdev->dev, "floodlight_adc");
  321. if (IS_ERR(pflash_led_dev->floodlight_adc)) {
  322. pr_err("not define iio floodlight_adc\n");
  323. }
  324. return 0;
  325. }
  326. static volatile uint64_t falling_time_us[2] = {0};
  327. uint64_t touch_pin_int_num(struct flash_led_ctrl *pflash_led_dev)
  328. {
  329. return pflash_led_dev->frame_mark->frame_irq_cnt;
  330. }
  331. void frame_irq_cnt_clear(struct flash_led_ctrl *pflash_led_dev)
  332. {
  333. pflash_led_dev->frame_mark->frame_irq_cnt = 0;
  334. }
  335. uint64_t touch_pin_high_time_us(void)
  336. {
  337. return 0;
  338. }
  339. uint64_t touch_pin_falling_int(void)
  340. {
  341. return 0;
  342. }
  343. uint64_t touch_pin_rising_int(void)
  344. {
  345. return 0;
  346. }
  347. uint64_t touch_pin_preiod_time_us(void)
  348. {
  349. if (falling_time_us[0] < falling_time_us[1]) {
  350. return falling_time_us[1] - falling_time_us[0];
  351. }
  352. return falling_time_us[0] - falling_time_us[1];
  353. }
  354. uint64_t get_us_time(void)
  355. {
  356. struct timespec64 ts;
  357. static uint64_t us = 0;
  358. ktime_get_real_ts64(&ts);
  359. us = timespec64_to_ns(&ts) / 1000UL;
  360. return us;
  361. }
  362. static void flash_led_interrupt_func(struct work_struct *work)
  363. {
  364. struct flash_led_ctrl *pflash_led_dev = container_of(work, struct flash_led_ctrl, flash_led_work);
  365. struct flash_led_dev *floodlight = &pflash_led_dev->floodlight;
  366. struct flash_led_dev *projection = &pflash_led_dev->projection;
  367. frame_mark_t *frame_mark = pflash_led_dev->frame_mark;
  368. uint64_t frame_irq_cnt;
  369. frame_mark->frame_irq_cnt += 1;
  370. frame_irq_cnt = frame_mark->frame_irq_cnt;
  371. frame_mark->frame_time_us = get_us_time();
  372. falling_time_us[frame_irq_cnt % 2] = frame_mark->frame_time_us;
  373. if (!pflash_led_dev->enable) {
  374. if (projection->flash_led_func != NULL) {
  375. projection->flash_led_func->disable_channel(projection, 3);
  376. }
  377. if (floodlight->flash_led_func != NULL) {
  378. floodlight->flash_led_func->disable_channel(floodlight, 3);
  379. }
  380. return;
  381. }
  382. flash_led_switch(pflash_led_dev,frame_irq_cnt);
  383. if ((frame_irq_cnt % 2) == 0) {
  384. if (!IS_ERR(pflash_led_dev->floodlight_adc)) {
  385. iio_read_channel_processed(pflash_led_dev->floodlight_adc,
  386. &frame_mark->floodlight_temperature);
  387. frame_mark->floodlight_temperature = get_ntc_temperature(frame_mark->floodlight_temperature);
  388. }
  389. } else {
  390. if (!IS_ERR(pflash_led_dev->projection_adc)) {
  391. iio_read_channel_processed(pflash_led_dev->projection_adc,
  392. &frame_mark->projection_temperature);
  393. frame_mark->projection_temperature = get_ntc_temperature(frame_mark->projection_temperature);
  394. }
  395. }
  396. }
  397. static irqreturn_t touch_pin_isr(int irq, void *dev)
  398. {
  399. struct flash_led_ctrl *pflash_led_dev = dev;
  400. schedule_work(&pflash_led_dev->flash_led_work);
  401. return IRQ_HANDLED;
  402. }
  403. static int flash_pin_init(struct flash_led_ctrl *dev)
  404. {
  405. gpio_request(dev->touch_pin, "flash led touch pin");
  406. INIT_WORK(&dev->flash_led_work, flash_led_interrupt_func);
  407. if (dev->floodlight_en_pin != -1) {
  408. gpio_request(dev->floodlight_en_pin, "floodlight enable pin");
  409. if (gpio_is_valid(dev->floodlight_en_pin)) {
  410. gpio_direction_output(dev->floodlight_en_pin, 1);
  411. }
  412. }
  413. if (dev->projection_en_pin != -1) {
  414. gpio_request(dev->projection_en_pin, "projection enable pin");
  415. if (gpio_is_valid(dev->projection_en_pin)) {
  416. gpio_direction_output(dev->projection_en_pin, 1);
  417. }
  418. }
  419. return 0;
  420. }
  421. static int touch_pin_uinit(struct flash_led_ctrl *dev)
  422. {
  423. dev->enable = 0;
  424. cancel_work_sync(&dev->flash_led_work);
  425. gpio_free(dev->touch_pin);
  426. return 0;
  427. }
  428. static int flash_led_probe(struct platform_device *pdev)
  429. {
  430. int ret = 0;
  431. struct flash_led_driver_dev *pdriver_dev;
  432. struct flash_led_ctrl *pflash_led_dev;
  433. struct device_node *np = pdev->dev.of_node;
  434. pr_info("enter %s\n", __func__);
  435. if (pdev->id >= FLASH_LED_MAXCNT) {
  436. pr_err("%s:pdev id is %d error\n", __func__,pdev->id);
  437. return -EINVAL;
  438. }
  439. pdev->id = of_alias_get_id(np, "flash_led");
  440. pdriver_dev = devm_kzalloc(&pdev->dev,sizeof(struct flash_led_driver_dev), GFP_KERNEL);
  441. if (pdriver_dev == NULL) {
  442. pr_err("%s:alloc struct flash_led_driver_dev error\n", __func__);
  443. return -ENOMEM;
  444. }
  445. memset(pdriver_dev,0,sizeof(struct flash_led_driver_dev ));
  446. pr_info("%s:flash_led[%d]: pdriver_dev =0x%px\n", __func__, pdev->id, pdriver_dev);
  447. pflash_led_dev = devm_kzalloc(&pdev->dev,sizeof(*pflash_led_dev), GFP_KERNEL);
  448. if (pflash_led_dev == NULL) {
  449. pr_err("%s:alloc struct flash_led_dev error\n", __func__);
  450. return -ENOMEM;
  451. }
  452. memset(pflash_led_dev,0,sizeof(*pflash_led_dev));
  453. pr_info("%s:flash_led[%d]: pflash_led_dev =0x%px\n", __func__,pdev->id,pflash_led_dev);
  454. pflash_led_dev->device_idx = pdev->id;
  455. pdriver_dev->private = pflash_led_dev;
  456. mutex_init(&pdriver_dev->vvmutex);
  457. frame_mark_t *frame_mark;
  458. frame_mark = dma_alloc_coherent(&pdev->dev, sizeof(frame_mark_t), &pflash_led_dev->frame_mark_info_addr, GFP_KERNEL);
  459. if (frame_mark == NULL ) {
  460. pr_err("dma_alloc_coherent error\n");
  461. return -1;
  462. }
  463. memset(frame_mark, 0, sizeof(frame_mark_t));
  464. pflash_led_dev->frame_mark = frame_mark;
  465. platform_set_drvdata(pdev, pdriver_dev);
  466. ret = flash_led_of_parse(pdev);
  467. if (ret < 0) {
  468. pr_err("%s:flash_led_of_parse error\n", __func__);
  469. return ret;
  470. }
  471. ret = flash_pin_init(pflash_led_dev);
  472. if (ret != 0) {
  473. pr_err("%s:flash_pin_init error\n", __func__);
  474. }
  475. ret = flash_led_init(pflash_led_dev);
  476. if (ret != 0) {
  477. pr_warn("%s:vvnative_flash_led_init error\n", __func__);
  478. }
  479. if (devise_register_index == 0) {
  480. if (flash_led_major == 0) {
  481. ret = alloc_chrdev_region(&pdriver_dev->devt, 0, FLASH_LED_MAXCNT, FLASH_LED_NAME);
  482. if (ret != 0) {
  483. pr_err("%s:alloc_chrdev_region error\n", __func__);
  484. return ret;
  485. }
  486. flash_led_major = MAJOR(pdriver_dev->devt);
  487. flash_led_minor = MINOR(pdriver_dev->devt);
  488. } else {
  489. pdriver_dev->devt = MKDEV(flash_led_major, flash_led_minor);
  490. ret = register_chrdev_region(pdriver_dev->devt, FLASH_LED_MAXCNT, FLASH_LED_NAME);
  491. if (ret) {
  492. pr_err("%s:register_chrdev_region error\n", __func__);
  493. return ret;
  494. }
  495. }
  496. flash_led_class = class_create(THIS_MODULE, FLASH_LED_NAME);
  497. if (IS_ERR(flash_led_class)) {
  498. pr_err("%s[%d]:class_create error!\n", __func__, __LINE__);
  499. return -EINVAL;
  500. }
  501. }
  502. pdriver_dev->devt = MKDEV(flash_led_major, flash_led_minor + pdev->id);
  503. cdev_init(&pdriver_dev->cdev, &flash_led_fops);
  504. ret = cdev_add(&pdriver_dev->cdev, pdriver_dev->devt, 1);
  505. if (ret) {
  506. pr_err("%s[%d]:cdev_add error!\n", __func__, __LINE__);
  507. return ret;
  508. }
  509. pdriver_dev->class = flash_led_class;
  510. device_create(pdriver_dev->class, NULL, pdriver_dev->devt,
  511. pdriver_dev, "%s%d", FLASH_LED_NAME, pdev->id);
  512. extern int flash_led_create_capabilities_sysfs(struct platform_device *pdev);
  513. flash_led_create_capabilities_sysfs(pdev);
  514. devise_register_index++;
  515. pr_info("exit %s\n", __func__);
  516. return ret;
  517. }
  518. static int flash_led_remove(struct platform_device *pdev)
  519. {
  520. struct flash_led_driver_dev *pdriver_dev;
  521. struct flash_led_ctrl *pflash_led_dev;
  522. pr_info("enter %s\n", __func__);
  523. devise_register_index--;
  524. pdriver_dev = platform_get_drvdata(pdev);
  525. if (pdriver_dev == NULL) {
  526. pr_err("%s:file private is null point error\n", __func__);
  527. return -ENOMEM;
  528. }
  529. pflash_led_dev = pdriver_dev->private;
  530. flash_led_deinit(pflash_led_dev);
  531. touch_pin_uinit(pflash_led_dev);
  532. dma_free_coherent(&pdev->dev, sizeof(*pflash_led_dev->frame_mark), pflash_led_dev->frame_mark, pflash_led_dev->frame_mark_info_addr);
  533. if (!IS_ERR(pflash_led_dev->projection_adc)) {
  534. iio_channel_release(pflash_led_dev->projection_adc);
  535. }
  536. if (!IS_ERR(pflash_led_dev->floodlight_adc)) {
  537. iio_channel_release(pflash_led_dev->floodlight_adc);
  538. }
  539. extern int flash_led_remove_capabilities_sysfs(struct platform_device *pdev);
  540. flash_led_remove_capabilities_sysfs(pdev);
  541. cdev_del(&pdriver_dev->cdev);
  542. device_destroy(pdriver_dev->class, pdriver_dev->devt);
  543. unregister_chrdev_region(pdriver_dev->devt, FLASH_LED_MAXCNT);
  544. if (devise_register_index == 0) {
  545. class_destroy(pdriver_dev->class);
  546. }
  547. devm_kfree(&pdev->dev, pdriver_dev);
  548. devm_kfree(&pdev->dev, pflash_led_dev);
  549. pr_info("exit %s\n", __func__);
  550. return 0;
  551. }
  552. static const struct of_device_id flash_led_of_match[] = {
  553. {.compatible = "thead,light-vvcam-flash_led"},
  554. };
  555. static struct platform_driver flash_led_driver = {
  556. .probe = flash_led_probe,
  557. .remove = flash_led_remove,
  558. .driver = {
  559. .name = FLASH_LED_NAME,
  560. .owner = THIS_MODULE,
  561. .of_match_table = of_match_ptr(flash_led_of_match),
  562. }
  563. };
  564. /*
  565. static void flash_led_pdev_release(struct device *dev)
  566. {
  567. pr_info("enter %s\n", __func__);
  568. }
  569. */
  570. static int __init flash_led_init_module(void)
  571. {
  572. int ret = 0;
  573. pr_info("enter %s\n", __func__);
  574. ret = platform_driver_register(&flash_led_driver);
  575. if (ret) {
  576. pr_err("register platform driver failed.\n");
  577. return ret;
  578. }
  579. return ret;
  580. }
  581. static void __exit flash_led_exit_module(void)
  582. {
  583. pr_info("enter %s\n", __func__);
  584. platform_driver_unregister(&flash_led_driver);
  585. }
  586. module_init(flash_led_init_module);
  587. module_exit(flash_led_exit_module);
  588. MODULE_DESCRIPTION("FLASH_LED");
  589. MODULE_LICENSE("GPL");