flash_led_driver.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  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_ON:
  120. projection_on = true;
  121. floodlight_on = true;
  122. break;
  123. case BOTH_OFF:
  124. projection_on = false;
  125. floodlight_on = false;
  126. break;
  127. default:
  128. pr_warn("invald switch mode:%d\n",pflash_led_dev->switch_mode);
  129. return -1;
  130. }
  131. if(projection->flash_led_func)
  132. {
  133. if(projection_on ==true && (pflash_led_dev->enable & PROJECTION_EN))
  134. projection->flash_led_func->enable_channel(projection, 3);
  135. else
  136. projection->flash_led_func->disable_channel(projection, 3);
  137. }
  138. if(floodlight->flash_led_func)
  139. {
  140. if(floodlight_on == true && (pflash_led_dev->enable & FLOODLIGHT_EN))
  141. floodlight->flash_led_func->enable_channel(floodlight, 3);
  142. else
  143. floodlight->flash_led_func->disable_channel(floodlight, 3);
  144. }
  145. return 0;
  146. }
  147. static int flash_led_open(struct inode * inode, struct file * file)
  148. {
  149. int ret = 0;
  150. int irq = 0;
  151. struct flash_led_driver_dev *pdriver_dev;
  152. struct flash_led_ctrl *pflash_led_dev;
  153. pdriver_dev = container_of(inode->i_cdev, struct flash_led_driver_dev, cdev);
  154. file->private_data = pdriver_dev;
  155. pflash_led_dev = (struct flash_led_ctrl*)pdriver_dev->private;
  156. struct flash_led_dev *floodlight = &pflash_led_dev->floodlight;
  157. struct flash_led_dev *projection = &pflash_led_dev->projection;
  158. if (pflash_led_dev->touch_pin != -1) {
  159. irq = gpio_to_irq(pflash_led_dev->touch_pin);
  160. request_irq(irq, touch_pin_isr, IRQF_TRIGGER_FALLING,
  161. "flash led touch pin",
  162. pflash_led_dev);
  163. }
  164. if (floodlight->flash_led_func != NULL) {
  165. ret = floodlight->flash_led_func->init(floodlight);
  166. if (ret != 0) {
  167. pr_err("%s, %d, floodlight init error\n", __func__, __LINE__);
  168. return ret;
  169. }
  170. }
  171. if (projection->flash_led_func != NULL) {
  172. ret = projection->flash_led_func->init(projection);
  173. if (ret != 0) {
  174. pr_err("%s, %d, projection init error\n", __func__, __LINE__);
  175. return ret;
  176. }
  177. }
  178. return 0;
  179. };
  180. static int flash_led_release(struct inode * inode, struct file * file)
  181. {
  182. int irq = -1;
  183. struct flash_led_driver_dev *pdriver_dev;
  184. pdriver_dev = container_of(inode->i_cdev, struct flash_led_driver_dev, cdev);
  185. struct flash_led_ctrl *pflash_led_dev = (struct flash_led_ctrl*)pdriver_dev->private;
  186. struct flash_led_dev *floodlight = &pflash_led_dev->floodlight;
  187. struct flash_led_dev *projection = &pflash_led_dev->projection;
  188. if (pflash_led_dev->touch_pin != -1) {
  189. irq = gpio_to_irq(pflash_led_dev->touch_pin);
  190. free_irq(irq, pflash_led_dev);
  191. }
  192. pflash_led_dev->enable = 0;
  193. if (projection->flash_led_func != NULL) {
  194. projection->flash_led_func->disable_channel(projection, 3);
  195. }
  196. if (floodlight->flash_led_func != NULL) {
  197. floodlight->flash_led_func->disable_channel(floodlight, 3);
  198. }
  199. return 0;
  200. };
  201. static long flash_led_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  202. {
  203. long ret = 0;
  204. struct flash_led_driver_dev *pdriver_dev;
  205. struct flash_led_ctrl *pflash_led_dev;
  206. pdriver_dev = file->private_data;
  207. if (pdriver_dev == NULL)
  208. {
  209. pr_err("%s:file private is null point error\n", __func__);
  210. return -ENOMEM;
  211. }
  212. pflash_led_dev = (struct flash_led_ctrl*)pdriver_dev->private;
  213. mutex_lock(&pdriver_dev->vvmutex);
  214. ret = flash_led_priv_ioctl(pflash_led_dev, cmd, (void __user *)arg);
  215. mutex_unlock(&pdriver_dev->vvmutex);
  216. return ret;
  217. };
  218. struct file_operations flash_led_fops = {
  219. .owner = THIS_MODULE,
  220. .open = flash_led_open,
  221. .release = flash_led_release,
  222. .unlocked_ioctl = flash_led_ioctl,
  223. };
  224. static int flash_led_of_parse(struct platform_device *pdev)
  225. {
  226. int ret = 0, regulator_num = 0;
  227. struct device_node *np = pdev->dev.of_node;
  228. struct flash_led_driver_dev *pdriver_dev = platform_get_drvdata(pdev);
  229. struct flash_led_ctrl *pflash_led_dev = pdriver_dev->private;
  230. struct flash_led_dev *floodlight = &pflash_led_dev->floodlight;
  231. struct flash_led_dev *projection = &pflash_led_dev->projection;
  232. ret = of_property_read_string(np, "flash_led_name", &pflash_led_dev->flash_led_name);
  233. if (ret < 0) {
  234. pr_err("%s:property flash_led_name not defined for %s\n", __func__, pdev->name);
  235. return ret;
  236. }
  237. // the flash led touch pin used touch the io interrupt
  238. pflash_led_dev->touch_pin = of_get_named_gpio(np, "flash_led_touch_pin", 0);
  239. if (pflash_led_dev->touch_pin >= 0) {
  240. ret = devm_gpio_request(&pdev->dev, pflash_led_dev->touch_pin,
  241. "flash_led_touch_pin");
  242. if (ret < 0) {
  243. pr_err("%s:flash_led_touch request failed\n", __func__);
  244. }
  245. } else {
  246. pflash_led_dev->touch_pin = -1;
  247. pr_err("flash_led_touch not defined for %s\n", pflash_led_dev->flash_led_name);
  248. }
  249. pflash_led_dev->floodlight_en_pin = of_get_named_gpio(np, "floodlight_en_pin", 0);
  250. if (pflash_led_dev->floodlight_en_pin >= 0) {
  251. ret = devm_gpio_request(&pdev->dev, pflash_led_dev->floodlight_en_pin,
  252. "floodlight_en_pin");
  253. if (ret < 0) {
  254. pr_err("%s:floodlight_en_pin request failed\n", __func__);
  255. }
  256. } else {
  257. pflash_led_dev->floodlight_en_pin = -1;
  258. pr_err("floodlight_en_pin not defined for %s\n", pflash_led_dev->flash_led_name);
  259. }
  260. pflash_led_dev->projection_en_pin = of_get_named_gpio(np, "projection_en_pin", 0);
  261. if (pflash_led_dev->projection_en_pin >= 0) {
  262. ret = devm_gpio_request(&pdev->dev, pflash_led_dev->projection_en_pin,
  263. "projection_en_pin");
  264. if (ret < 0) {
  265. pr_err("%s:projection_en_pin request failed\n", __func__);
  266. }
  267. } else {
  268. pflash_led_dev->projection_en_pin = -1;
  269. pr_err("projection_en_pin not defined for %s\n", pflash_led_dev->flash_led_name);
  270. }
  271. floodlight->regulators.num = of_property_count_strings(np, "floodlight_regulators");
  272. if (floodlight->regulators.num <= 0) {
  273. pr_info("%s:property floodlight_regulators not defined for %s\n", __func__, pdev->name);
  274. } else {
  275. pr_info("%s num_of_regulators %d\n", __func__, floodlight->regulators.num);
  276. ret = of_property_read_string_array(np, "floodlight_regulators",
  277. floodlight->regulators.name, floodlight->regulators.num);
  278. if (ret != floodlight->regulators.num) {
  279. pr_err("%s:fail to read property floodlight_regulators\n", __func__);
  280. return -1;
  281. };
  282. regulator_num = floodlight->regulators.num;
  283. while (regulator_num) {
  284. floodlight->regulators.supply[regulator_num - 1] = devm_regulator_get(&pdev->dev,
  285. floodlight->regulators.name[regulator_num - 1]);
  286. if (IS_ERR(floodlight->regulators.supply[regulator_num - 1])) {
  287. pr_err("%s:fail to devm_regulator_get %s\n", __func__, floodlight->regulators.name[regulator_num - 1]);
  288. return -1;
  289. }
  290. regulator_num--;
  291. }
  292. }
  293. projection->regulators.num = of_property_count_strings(np, "projection_regulators");
  294. if (projection->regulators.num <= 0) {
  295. pr_info("%s:property projection_regulators not defined for %s\n", __func__, pdev->name);
  296. } else {
  297. pr_info("%s num_of_regulators %d\n", __func__, projection->regulators.num);
  298. ret = of_property_read_string_array(np, "projection_regulators",
  299. projection->regulators.name, projection->regulators.num);
  300. if (ret != projection->regulators.num) {
  301. pr_err("%s:fail to read property projection_regulators\n", __func__);
  302. return -1;
  303. };
  304. regulator_num = projection->regulators.num;
  305. while (regulator_num) {
  306. projection->regulators.supply[regulator_num - 1] = devm_regulator_get(&pdev->dev,
  307. projection->regulators.name[regulator_num - 1]);
  308. if (IS_ERR(projection->regulators.supply[regulator_num - 1])) {
  309. pr_err("%s:fail to devm_regulator_get %s\n", __func__, projection->regulators.name[regulator_num - 1]);
  310. return -1;
  311. }
  312. regulator_num--;
  313. }
  314. }
  315. ret = of_property_read_u8(np, "floodlight_i2c_bus",
  316. &floodlight->i2c_bus);
  317. if (ret != 0) {
  318. pr_err("fail to read property floodlight_i2c_bus\n");
  319. floodlight->i2c_bus = UNDEFINED_IN_DTS;
  320. }
  321. ret = of_property_read_u8(np, "projection_i2c_bus",
  322. &projection->i2c_bus);
  323. if (ret != 0) {
  324. pr_err("fail to read property projection_i2c_bus\n");
  325. projection->i2c_bus = UNDEFINED_IN_DTS;
  326. }
  327. pflash_led_dev->projection_adc = iio_channel_get(&pdev->dev, "projection_adc");
  328. if (IS_ERR(pflash_led_dev->projection_adc)) {
  329. pr_err("not define projection_adc adc\n");
  330. }
  331. pflash_led_dev->floodlight_adc = iio_channel_get(&pdev->dev, "floodlight_adc");
  332. if (IS_ERR(pflash_led_dev->floodlight_adc)) {
  333. pr_err("not define iio floodlight_adc\n");
  334. }
  335. return 0;
  336. }
  337. static volatile uint64_t falling_time_us[2] = {0};
  338. uint64_t touch_pin_int_num(struct flash_led_ctrl *pflash_led_dev)
  339. {
  340. return pflash_led_dev->frame_mark->frame_irq_cnt;
  341. }
  342. void frame_irq_cnt_clear(struct flash_led_ctrl *pflash_led_dev)
  343. {
  344. pflash_led_dev->frame_mark->frame_irq_cnt = 0;
  345. }
  346. uint64_t touch_pin_high_time_us(void)
  347. {
  348. return 0;
  349. }
  350. uint64_t touch_pin_falling_int(void)
  351. {
  352. return 0;
  353. }
  354. uint64_t touch_pin_rising_int(void)
  355. {
  356. return 0;
  357. }
  358. uint64_t touch_pin_preiod_time_us(void)
  359. {
  360. if (falling_time_us[0] < falling_time_us[1]) {
  361. return falling_time_us[1] - falling_time_us[0];
  362. }
  363. return falling_time_us[0] - falling_time_us[1];
  364. }
  365. uint64_t get_us_time(void)
  366. {
  367. struct timespec64 ts;
  368. static uint64_t us = 0;
  369. ktime_get_real_ts64(&ts);
  370. us = timespec64_to_ns(&ts) / 1000UL;
  371. return us;
  372. }
  373. static void flash_led_interrupt_func(struct work_struct *work)
  374. {
  375. struct flash_led_ctrl *pflash_led_dev = container_of(work, struct flash_led_ctrl, flash_led_work);
  376. struct flash_led_dev *floodlight = &pflash_led_dev->floodlight;
  377. struct flash_led_dev *projection = &pflash_led_dev->projection;
  378. frame_mark_t *frame_mark = pflash_led_dev->frame_mark;
  379. uint64_t frame_irq_cnt;
  380. frame_mark->frame_irq_cnt += 1;
  381. frame_irq_cnt = frame_mark->frame_irq_cnt;
  382. frame_mark->frame_time_us = get_us_time();
  383. falling_time_us[frame_irq_cnt % 2] = frame_mark->frame_time_us;
  384. if (!pflash_led_dev->enable) {
  385. if (projection->flash_led_func != NULL) {
  386. projection->flash_led_func->disable_channel(projection, 3);
  387. }
  388. if (floodlight->flash_led_func != NULL) {
  389. floodlight->flash_led_func->disable_channel(floodlight, 3);
  390. }
  391. return;
  392. }
  393. if (pflash_led_dev->switch_mode != BOTH_ON \
  394. && pflash_led_dev->switch_mode != BOTH_OFF) {
  395. flash_led_switch(pflash_led_dev,frame_irq_cnt);
  396. }
  397. if ((frame_irq_cnt % 2) == 0) {
  398. if (!IS_ERR(pflash_led_dev->floodlight_adc)) {
  399. iio_read_channel_processed(pflash_led_dev->floodlight_adc,
  400. &frame_mark->floodlight_temperature);
  401. frame_mark->floodlight_temperature = get_ntc_temperature(frame_mark->floodlight_temperature);
  402. }
  403. } else {
  404. if (!IS_ERR(pflash_led_dev->projection_adc)) {
  405. iio_read_channel_processed(pflash_led_dev->projection_adc,
  406. &frame_mark->projection_temperature);
  407. frame_mark->projection_temperature = get_ntc_temperature(frame_mark->projection_temperature);
  408. }
  409. }
  410. }
  411. static irqreturn_t touch_pin_isr(int irq, void *dev)
  412. {
  413. struct flash_led_ctrl *pflash_led_dev = dev;
  414. schedule_work(&pflash_led_dev->flash_led_work);
  415. return IRQ_HANDLED;
  416. }
  417. static int flash_pin_init(struct flash_led_ctrl *dev)
  418. {
  419. if (dev->touch_pin != -1) {
  420. gpio_request(dev->touch_pin, "flash led touch pin");
  421. }
  422. INIT_WORK(&dev->flash_led_work, flash_led_interrupt_func);
  423. if (dev->floodlight_en_pin != -1) {
  424. gpio_request(dev->floodlight_en_pin, "floodlight enable pin");
  425. if (gpio_is_valid(dev->floodlight_en_pin)) {
  426. gpio_direction_output(dev->floodlight_en_pin, 1);
  427. }
  428. }
  429. if (dev->projection_en_pin != -1) {
  430. gpio_request(dev->projection_en_pin, "projection enable pin");
  431. if (gpio_is_valid(dev->projection_en_pin)) {
  432. gpio_direction_output(dev->projection_en_pin, 1);
  433. }
  434. }
  435. return 0;
  436. }
  437. static int touch_pin_uinit(struct flash_led_ctrl *dev)
  438. {
  439. dev->enable = 0;
  440. cancel_work_sync(&dev->flash_led_work);
  441. if (dev->touch_pin != -1) {
  442. gpio_free(dev->touch_pin);
  443. }
  444. return 0;
  445. }
  446. static int flash_led_probe(struct platform_device *pdev)
  447. {
  448. int ret = 0;
  449. struct flash_led_driver_dev *pdriver_dev;
  450. struct flash_led_ctrl *pflash_led_dev;
  451. struct device_node *np = pdev->dev.of_node;
  452. pr_info("enter %s\n", __func__);
  453. if (pdev->id >= FLASH_LED_MAXCNT) {
  454. pr_err("%s:pdev id is %d error\n", __func__,pdev->id);
  455. return -EINVAL;
  456. }
  457. pdev->id = of_alias_get_id(np, "flash_led");
  458. pdriver_dev = devm_kzalloc(&pdev->dev,sizeof(struct flash_led_driver_dev), GFP_KERNEL);
  459. if (pdriver_dev == NULL) {
  460. pr_err("%s:alloc struct flash_led_driver_dev error\n", __func__);
  461. return -ENOMEM;
  462. }
  463. memset(pdriver_dev,0,sizeof(struct flash_led_driver_dev ));
  464. pr_info("%s:flash_led[%d]: pdriver_dev =0x%px\n", __func__, pdev->id, pdriver_dev);
  465. pflash_led_dev = devm_kzalloc(&pdev->dev,sizeof(*pflash_led_dev), GFP_KERNEL);
  466. if (pflash_led_dev == NULL) {
  467. pr_err("%s:alloc struct flash_led_dev error\n", __func__);
  468. return -ENOMEM;
  469. }
  470. memset(pflash_led_dev,0,sizeof(*pflash_led_dev));
  471. pr_info("%s:flash_led[%d]: pflash_led_dev =0x%px\n", __func__,pdev->id,pflash_led_dev);
  472. pflash_led_dev->device_idx = pdev->id;
  473. pdriver_dev->private = pflash_led_dev;
  474. mutex_init(&pdriver_dev->vvmutex);
  475. frame_mark_t *frame_mark;
  476. frame_mark = dma_alloc_coherent(&pdev->dev, sizeof(frame_mark_t), &pflash_led_dev->frame_mark_info_addr, GFP_KERNEL);
  477. if (frame_mark == NULL ) {
  478. pr_err("dma_alloc_coherent error\n");
  479. return -1;
  480. }
  481. memset(frame_mark, 0, sizeof(frame_mark_t));
  482. pflash_led_dev->frame_mark = frame_mark;
  483. platform_set_drvdata(pdev, pdriver_dev);
  484. ret = flash_led_of_parse(pdev);
  485. if (ret < 0) {
  486. pr_err("%s:flash_led_of_parse error\n", __func__);
  487. return ret;
  488. }
  489. ret = flash_pin_init(pflash_led_dev);
  490. if (ret != 0) {
  491. pr_err("%s:flash_pin_init error\n", __func__);
  492. }
  493. ret = flash_led_init(pflash_led_dev);
  494. if (ret != 0) {
  495. pr_warn("%s:vvnative_flash_led_init error\n", __func__);
  496. }
  497. if (devise_register_index == 0) {
  498. if (flash_led_major == 0) {
  499. ret = alloc_chrdev_region(&pdriver_dev->devt, 0, FLASH_LED_MAXCNT, FLASH_LED_NAME);
  500. if (ret != 0) {
  501. pr_err("%s:alloc_chrdev_region error\n", __func__);
  502. return ret;
  503. }
  504. flash_led_major = MAJOR(pdriver_dev->devt);
  505. flash_led_minor = MINOR(pdriver_dev->devt);
  506. } else {
  507. pdriver_dev->devt = MKDEV(flash_led_major, flash_led_minor);
  508. ret = register_chrdev_region(pdriver_dev->devt, FLASH_LED_MAXCNT, FLASH_LED_NAME);
  509. if (ret) {
  510. pr_err("%s:register_chrdev_region error\n", __func__);
  511. return ret;
  512. }
  513. }
  514. flash_led_class = class_create(THIS_MODULE, FLASH_LED_NAME);
  515. if (IS_ERR(flash_led_class)) {
  516. pr_err("%s[%d]:class_create error!\n", __func__, __LINE__);
  517. return -EINVAL;
  518. }
  519. }
  520. pdriver_dev->devt = MKDEV(flash_led_major, flash_led_minor + pdev->id);
  521. cdev_init(&pdriver_dev->cdev, &flash_led_fops);
  522. ret = cdev_add(&pdriver_dev->cdev, pdriver_dev->devt, 1);
  523. if (ret) {
  524. pr_err("%s[%d]:cdev_add error!\n", __func__, __LINE__);
  525. return ret;
  526. }
  527. pdriver_dev->class = flash_led_class;
  528. device_create(pdriver_dev->class, NULL, pdriver_dev->devt,
  529. pdriver_dev, "%s%d", FLASH_LED_NAME, pdev->id);
  530. extern int flash_led_create_capabilities_sysfs(struct platform_device *pdev);
  531. flash_led_create_capabilities_sysfs(pdev);
  532. devise_register_index++;
  533. pr_info("exit %s\n", __func__);
  534. return ret;
  535. }
  536. static int flash_led_remove(struct platform_device *pdev)
  537. {
  538. struct flash_led_driver_dev *pdriver_dev;
  539. struct flash_led_ctrl *pflash_led_dev;
  540. pr_info("enter %s\n", __func__);
  541. devise_register_index--;
  542. pdriver_dev = platform_get_drvdata(pdev);
  543. if (pdriver_dev == NULL) {
  544. pr_err("%s:file private is null point error\n", __func__);
  545. return -ENOMEM;
  546. }
  547. pflash_led_dev = pdriver_dev->private;
  548. flash_led_deinit(pflash_led_dev);
  549. touch_pin_uinit(pflash_led_dev);
  550. dma_free_coherent(&pdev->dev, sizeof(*pflash_led_dev->frame_mark), pflash_led_dev->frame_mark, pflash_led_dev->frame_mark_info_addr);
  551. if (!IS_ERR(pflash_led_dev->projection_adc)) {
  552. iio_channel_release(pflash_led_dev->projection_adc);
  553. }
  554. if (!IS_ERR(pflash_led_dev->floodlight_adc)) {
  555. iio_channel_release(pflash_led_dev->floodlight_adc);
  556. }
  557. extern int flash_led_remove_capabilities_sysfs(struct platform_device *pdev);
  558. flash_led_remove_capabilities_sysfs(pdev);
  559. cdev_del(&pdriver_dev->cdev);
  560. device_destroy(pdriver_dev->class, pdriver_dev->devt);
  561. unregister_chrdev_region(pdriver_dev->devt, FLASH_LED_MAXCNT);
  562. if (devise_register_index == 0) {
  563. class_destroy(pdriver_dev->class);
  564. }
  565. devm_kfree(&pdev->dev, pdriver_dev);
  566. devm_kfree(&pdev->dev, pflash_led_dev);
  567. pr_info("exit %s\n", __func__);
  568. return 0;
  569. }
  570. static const struct of_device_id flash_led_of_match[] = {
  571. {.compatible = "thead,light-vvcam-flash_led"},
  572. };
  573. static struct platform_driver flash_led_driver = {
  574. .probe = flash_led_probe,
  575. .remove = flash_led_remove,
  576. .driver = {
  577. .name = FLASH_LED_NAME,
  578. .owner = THIS_MODULE,
  579. .of_match_table = of_match_ptr(flash_led_of_match),
  580. }
  581. };
  582. /*
  583. static void flash_led_pdev_release(struct device *dev)
  584. {
  585. pr_info("enter %s\n", __func__);
  586. }
  587. */
  588. static int __init flash_led_init_module(void)
  589. {
  590. int ret = 0;
  591. pr_info("enter %s\n", __func__);
  592. ret = platform_driver_register(&flash_led_driver);
  593. if (ret) {
  594. pr_err("register platform driver failed.\n");
  595. return ret;
  596. }
  597. return ret;
  598. }
  599. static void __exit flash_led_exit_module(void)
  600. {
  601. pr_info("enter %s\n", __func__);
  602. platform_driver_unregister(&flash_led_driver);
  603. }
  604. module_init(flash_led_init_module);
  605. module_exit(flash_led_exit_module);
  606. MODULE_DESCRIPTION("FLASH_LED");
  607. MODULE_LICENSE("GPL");