vvcam_sensor_driver.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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/poll.h>
  68. #include "vvsensor.h"
  69. #include "sensor_common.h"
  70. #define VIVCAM_SENSOR_NAME "vivcam"
  71. #define VIVCAM_SENSOR_MAXCNT 30
  72. static unsigned int vvcam_sensor_major = 0;
  73. static unsigned int vvcam_sensor_minor = 0;
  74. struct class *vvcam_sensor_class;
  75. static unsigned int devise_register_index = 0;
  76. static int vvcam_sensor_open(struct inode * inode, struct file * file)
  77. {
  78. int ret = 0;
  79. struct vvcam_sensor_driver_dev *pdriver_dev;
  80. struct vvcam_sensor_dev * psensor_dev;
  81. pdriver_dev = container_of(inode->i_cdev, struct vvcam_sensor_driver_dev, cdev);
  82. file->private_data = pdriver_dev;
  83. psensor_dev = pdriver_dev->private;
  84. pr_debug("%s, enter: vvcam%d \n", __func__, psensor_dev->device_idx);
  85. if (gpio_is_valid(psensor_dev->rst_pin) && !psensor_dev->rst_pin_is_busy) {
  86. ret = devm_gpio_request(&pdriver_dev->pdev->dev, psensor_dev->rst_pin, "sensor_rst");
  87. if (ret < 0) {
  88. pr_err("%s:sensor_rst request failed\n", __func__);
  89. } else {
  90. psensor_dev->rst_pin_is_busy++;
  91. }
  92. }
  93. if (gpio_is_valid(psensor_dev->pdn_pin) && !psensor_dev->pdn_pin_is_busy) {
  94. ret = devm_gpio_request(&pdriver_dev->pdev->dev, psensor_dev->pdn_pin, "sensor_pdn");
  95. if (ret < 0) {
  96. pr_err("%s:sensor_pdn request failed\n", __func__);
  97. } else {
  98. psensor_dev->pdn_pin_is_busy++;
  99. }
  100. }
  101. pr_debug("%s, exit: vvcam%d \n", __func__, psensor_dev->device_idx);
  102. return 0;
  103. }
  104. static long vvcam_sensor_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  105. {
  106. long ret = 0;
  107. struct vvcam_sensor_driver_dev *pdriver_dev;
  108. struct vvcam_sensor_dev * psensor_dev;
  109. pdriver_dev = file->private_data;
  110. if (pdriver_dev == NULL)
  111. {
  112. pr_err("%s:file private is null point error\n", __func__);
  113. return -ENOMEM;
  114. }
  115. psensor_dev = pdriver_dev->private;
  116. pr_debug("%s:sensor[%d], cmd %x\n", __func__, psensor_dev->device_idx, cmd);
  117. mutex_lock(&pdriver_dev->vvmutex);
  118. ret = sensor_priv_ioctl(psensor_dev, cmd ,(void __user *)arg);
  119. mutex_unlock(&pdriver_dev->vvmutex);
  120. return ret;
  121. }
  122. static int vvcam_sensor_release(struct inode * inode, struct file * file)
  123. {
  124. struct vvcam_sensor_driver_dev *pdriver_dev;
  125. struct vvcam_sensor_dev * psensor_dev;
  126. pdriver_dev = container_of(inode->i_cdev, struct vvcam_sensor_driver_dev, cdev);
  127. if (pdriver_dev == NULL) {
  128. pr_err("%s:file private is null point error\n", __func__);
  129. return -ENOMEM;
  130. }
  131. psensor_dev = pdriver_dev->private;
  132. pr_debug("%s, enter: vvcam%d \n", __func__, psensor_dev->device_idx);
  133. psensor_dev->rst_pin_is_busy--;
  134. psensor_dev->pdn_pin_is_busy--;
  135. if (gpio_is_valid(psensor_dev->rst_pin) && psensor_dev->rst_pin_is_busy == 0) {
  136. gpio_free(psensor_dev->rst_pin);
  137. }
  138. if (gpio_is_valid(psensor_dev->pdn_pin) && psensor_dev->pdn_pin_is_busy == 0) {
  139. gpio_free(psensor_dev->pdn_pin);
  140. }
  141. pr_debug("%s, exit: vvcam%d \n", __func__, psensor_dev->device_idx);
  142. return 0;// sensor_reset(psensor_dev);
  143. }
  144. static unsigned vvcam_sensor_poll(struct file *file, poll_table *wait)
  145. {
  146. long ret = 0;
  147. unsigned int mask = 0;
  148. struct vvcam_sensor_driver_dev *pdriver_dev;
  149. struct vvcam_sensor_dev * psensor_dev;
  150. pdriver_dev = file->private_data;
  151. if (pdriver_dev == NULL)
  152. {
  153. pr_err("%s:file private is null point error\n", __func__);
  154. return -ENOMEM;
  155. }
  156. psensor_dev = pdriver_dev->private;
  157. poll_wait(file, &psensor_dev->err_wait, wait);
  158. if (psensor_dev->err_mask) {
  159. mask |= POLLIN | POLLRDNORM;
  160. psensor_dev->err_mask = 0;
  161. }
  162. return mask;
  163. }
  164. struct file_operations vvcam_sensor_fops = {
  165. .owner = THIS_MODULE,
  166. .open = vvcam_sensor_open,
  167. .release = vvcam_sensor_release,
  168. .unlocked_ioctl = vvcam_sensor_ioctl,
  169. .poll = vvcam_sensor_poll,
  170. };
  171. static int vvcam_sensor_of_parse(struct platform_device *pdev)
  172. {
  173. int ret = 0, regulator_num = 0, voltage_set_num = 0;
  174. struct device_node *np = pdev->dev.of_node;
  175. struct vvcam_sensor_driver_dev *pdriver_dev = platform_get_drvdata(pdev);
  176. struct vvcam_sensor_dev * psensor_dev = pdriver_dev->private;
  177. ret = of_property_read_string(np, "sensor_name", &psensor_dev->sensor_name);
  178. if (ret < 0) {
  179. pr_err("%s:property sensor_name not defined for %s\n", __func__, pdev->name);
  180. return ret;
  181. }
  182. psensor_dev->dev = &pdev->dev;
  183. psensor_dev->regulators.num = of_property_count_strings(np, "sensor_regulators");
  184. if (psensor_dev->regulators.num <= 0) {
  185. pr_err("%s:property sensor_regulators not defined for %s\n", __func__, pdev->name);
  186. } else {
  187. pr_info("%s num_of_regulators %d\n", __func__, psensor_dev->regulators.num);
  188. ret = of_property_read_string_array(np, "sensor_regulators",
  189. psensor_dev->regulators.name, psensor_dev->regulators.num);
  190. if (ret != psensor_dev->regulators.num) {
  191. pr_err("%s:fail to read property sensor_regulators\n", __func__);
  192. return -1;
  193. };
  194. voltage_set_num = of_property_count_u32_elems(np, "sensor_regulator_voltage_uV");
  195. if(voltage_set_num == psensor_dev->regulators.num){
  196. ret = of_property_read_u32_array(np, "sensor_regulator_voltage_uV",
  197. psensor_dev->regulators.voltage, psensor_dev->regulators.num);
  198. if (ret != 0) {
  199. pr_err("%s:fail to read property sensor_regulator_voltage_uV\n", __func__);
  200. return -1;
  201. }
  202. }
  203. ret = of_property_read_u32_array(np, "sensor_regulator_timing_us",
  204. psensor_dev->regulators.delay_us, psensor_dev->regulators.num);
  205. if (ret != 0) {
  206. pr_err("%s:fail to read property sensor_regulator_timing_us\n", __func__);
  207. return ret;
  208. }
  209. regulator_num = psensor_dev->regulators.num;
  210. while (regulator_num) {
  211. pr_info("%s regulator %s delay %d\n", psensor_dev->sensor_name, psensor_dev->regulators.name[regulator_num - 1],
  212. psensor_dev->regulators.delay_us[regulator_num - 1]);
  213. psensor_dev->regulators.supply[regulator_num - 1] = devm_regulator_get(&pdev->dev,
  214. psensor_dev->regulators.name[regulator_num - 1]);
  215. if (IS_ERR(psensor_dev->regulators.supply[regulator_num - 1])) {
  216. pr_err("%s:fail to devm_regulator_get %s\n", __func__, psensor_dev->regulators.name[regulator_num - 1]);
  217. return -1;
  218. }
  219. regulator_num--;
  220. }
  221. }
  222. psensor_dev->pdn_pin = of_get_named_gpio(np, "sensor_pdn", 0);
  223. if (psensor_dev->pdn_pin < 0) {
  224. pr_err("sensor_pdn not defined for %s\n", psensor_dev->sensor_name);
  225. }
  226. ret = of_property_read_u32(np, "sensor_pdn_delay_us",
  227. &psensor_dev->pdn_delay_us);
  228. if (ret != 0) {
  229. pr_err("fail to read property sensor_pdn_delay_us, assume zero\n");
  230. }
  231. psensor_dev->rst_pin = of_get_named_gpio(np, "sensor_rst", 0);
  232. if (psensor_dev->rst_pin < 0) {
  233. pr_err("sensor_rst not defined for %s\n", psensor_dev->sensor_name);
  234. }
  235. ret = of_property_read_u8(np, "i2c_addr",
  236. &psensor_dev->sensor_sccb_cfg.slave_addr);
  237. if (ret != 0) {
  238. pr_err("fail to read property i2c_addr, refer to %s.c\n", psensor_dev->sensor_name);
  239. }
  240. ret = of_property_read_u8(np, "i2c_reg_width",
  241. &psensor_dev->sensor_sccb_cfg.addr_byte);
  242. if (ret != 0) {
  243. pr_err("fail to read property i2c_reg_width, refer to %s.c\n", psensor_dev->sensor_name);
  244. }
  245. ret = of_property_read_u8(np, "i2c_data_width",
  246. &psensor_dev->sensor_sccb_cfg.data_byte);
  247. if (ret != 0) {
  248. pr_err("fail to read property i2c_data_width, refer to %s.c\n", psensor_dev->sensor_name);
  249. }
  250. ret = of_property_read_u8(np, "i2c_bus",
  251. &psensor_dev->i2c_bus);
  252. if (ret != 0) {
  253. pr_err("fail to read property i2c_bus, refer to %s.c\n", psensor_dev->sensor_name);
  254. psensor_dev->i2c_bus = UNDEFINED_IN_DTS;
  255. return -1;
  256. }
  257. return 0;
  258. }
  259. static int vvcam_sensor_probe(struct platform_device *pdev)
  260. {
  261. int ret = 0;
  262. struct vvcam_sensor_driver_dev *pdriver_dev;
  263. struct vvcam_sensor_dev * psensor_dev;
  264. struct device_node *np = pdev->dev.of_node;
  265. pr_debug("enter %s\n", __func__);
  266. pdev->id = of_alias_get_id(np, "vivcam");
  267. if (pdev->id >= VIVCAM_SENSOR_MAXCNT)
  268. {
  269. pr_err("%s:pdev id is %d error\n", __func__,pdev->id);
  270. return -EINVAL;
  271. }
  272. pdriver_dev = devm_kzalloc(&pdev->dev,sizeof(struct vvcam_sensor_driver_dev), GFP_KERNEL);
  273. if (pdriver_dev == NULL)
  274. {
  275. pr_err("%s:alloc struct vvcam_sensor_driver_dev error\n", __func__);
  276. return -ENOMEM;
  277. }
  278. memset(pdriver_dev,0,sizeof(struct vvcam_sensor_driver_dev ));
  279. pr_info("%s:sensor[%d]: pdriver_dev =0x%px\n", __func__,pdev->id,pdriver_dev);
  280. psensor_dev = devm_kzalloc(&pdev->dev,sizeof(struct vvcam_sensor_dev), GFP_KERNEL);
  281. if (psensor_dev == NULL)
  282. {
  283. pr_err("%s:alloc struct vvcam_sensor_dev error\n", __func__);
  284. return -ENOMEM;
  285. }
  286. memset(psensor_dev,0,sizeof(struct vvcam_sensor_dev ));
  287. pr_info("%s:sensor[%d]: psensor_dev =0x%px\n", __func__,pdev->id,psensor_dev);
  288. psensor_dev->device_idx = pdev->id;
  289. init_waitqueue_head(&psensor_dev->err_wait);
  290. pdriver_dev->private = psensor_dev;
  291. pdriver_dev->pdev = pdev;
  292. mutex_init(&pdriver_dev->vvmutex);
  293. platform_set_drvdata(pdev, pdriver_dev);
  294. ret = vvcam_sensor_of_parse(pdev);
  295. if (ret < 0)
  296. {
  297. pr_err("%s:vvcam_sensor_of_parse error\n", __func__);
  298. return ret;
  299. }
  300. ret = vvnative_sensor_init(psensor_dev);
  301. if (ret != 0)
  302. {
  303. pr_err("%s:vvnative_sensor_init error\n", __func__);
  304. return -EIO;
  305. }
  306. if (devise_register_index == 0)
  307. {
  308. if (vvcam_sensor_major == 0)
  309. {
  310. ret = alloc_chrdev_region(&pdriver_dev->devt, 0, VIVCAM_SENSOR_MAXCNT, VIVCAM_SENSOR_NAME);
  311. if (ret != 0)
  312. {
  313. pr_err("%s:alloc_chrdev_region error\n", __func__);
  314. return ret;
  315. }
  316. vvcam_sensor_major = MAJOR(pdriver_dev->devt);
  317. vvcam_sensor_minor = MINOR(pdriver_dev->devt);
  318. }
  319. else
  320. {
  321. pdriver_dev->devt = MKDEV(vvcam_sensor_major, vvcam_sensor_minor);
  322. ret = register_chrdev_region(pdriver_dev->devt, VIVCAM_SENSOR_MAXCNT, VIVCAM_SENSOR_NAME);
  323. if (ret)
  324. {
  325. pr_err("%s:register_chrdev_region error\n", __func__);
  326. return ret;
  327. }
  328. }
  329. vvcam_sensor_class = class_create(THIS_MODULE, VIVCAM_SENSOR_NAME);
  330. if (IS_ERR(vvcam_sensor_class))
  331. {
  332. pr_err("%s[%d]:class_create error!\n", __func__, __LINE__);
  333. return -EINVAL;
  334. }
  335. }
  336. pdriver_dev->devt = MKDEV(vvcam_sensor_major, vvcam_sensor_minor + pdev->id);
  337. cdev_init(&pdriver_dev->cdev, &vvcam_sensor_fops);
  338. ret = cdev_add(&pdriver_dev->cdev, pdriver_dev->devt, 1);
  339. if ( ret )
  340. {
  341. pr_err("%s[%d]:cdev_add error!\n", __func__, __LINE__);
  342. return ret;
  343. }
  344. pdriver_dev->class = vvcam_sensor_class;
  345. device_create(pdriver_dev->class, NULL, pdriver_dev->devt,
  346. pdriver_dev, "%s%d", VIVCAM_SENSOR_NAME, pdev->id);
  347. extern int sensor_create_capabilities_sysfs(struct platform_device *pdev);
  348. sensor_create_capabilities_sysfs(pdev);
  349. devise_register_index++;
  350. pr_debug("exit %s\n", __func__);
  351. return ret;
  352. }
  353. static int vvcam_sensor_remove(struct platform_device *pdev)
  354. {
  355. struct vvcam_sensor_driver_dev *pdriver_dev;
  356. struct vvcam_sensor_dev * psensor_dev;
  357. pr_debug("enter %s\n", __func__);
  358. devise_register_index--;
  359. pdriver_dev = platform_get_drvdata(pdev);
  360. if (pdriver_dev == NULL)
  361. {
  362. pr_err("%s:file private is null point error\n", __func__);
  363. return -ENOMEM;
  364. }
  365. psensor_dev = pdriver_dev->private;
  366. vvnative_sensor_deinit(psensor_dev);
  367. extern int sensor_remove_capabilities_sysfs(struct platform_device *pdev);
  368. sensor_remove_capabilities_sysfs(pdev);
  369. cdev_del(&pdriver_dev->cdev);
  370. device_destroy(pdriver_dev->class, pdriver_dev->devt);
  371. unregister_chrdev_region(pdriver_dev->devt, VIVCAM_SENSOR_MAXCNT);
  372. if (devise_register_index == 0)
  373. {
  374. class_destroy(pdriver_dev->class);
  375. }
  376. devm_kfree(&pdev->dev, pdriver_dev);
  377. devm_kfree(&pdev->dev, psensor_dev);
  378. return 0;
  379. }
  380. static const struct of_device_id vvcam_sensor_of_match[] = {
  381. {.compatible = "thead,light-vvcam-sensor"},
  382. };
  383. static struct platform_driver vvcam_sensor_driver = {
  384. .probe = vvcam_sensor_probe,
  385. .remove = vvcam_sensor_remove,
  386. .driver = {
  387. .name = VIVCAM_SENSOR_NAME,
  388. .owner = THIS_MODULE,
  389. .of_match_table = of_match_ptr(vvcam_sensor_of_match),
  390. }
  391. };
  392. static void vvcam_sensor_pdev_release(struct device *dev)
  393. {
  394. pr_debug("enter %s\n", __func__);
  395. }
  396. #if 0//def WITH_VVCAM
  397. static struct platform_device vvcam_sensor_pdev = {
  398. .name = VIVCAM_SENSOR_NAME,
  399. .id = 0,
  400. .dev.release = vvcam_sensor_pdev_release,
  401. };
  402. #endif
  403. #if 0//def WITH_VVCAM_DUAL
  404. static struct platform_device vvcam_sensor_dual_pdev = {
  405. .name = VIVCAM_SENSOR_NAME,
  406. .id = 1,
  407. .dev.release = vvcam_sensor_pdev_release,
  408. };
  409. #endif
  410. static int __init vvcam_sensor_init_module(void)
  411. {
  412. int ret = 0;
  413. pr_debug("enter %s\n", __func__);
  414. #if 0//def WITH_VVCAM
  415. ret = platform_device_register(&vvcam_sensor_pdev);
  416. if (ret)
  417. {
  418. pr_err("register platform device failed.\n");
  419. return ret;
  420. }
  421. #endif
  422. #if 0//def WITH_VVCAM_DUAL
  423. ret = platform_device_register(&vvcam_sensor_dual_pdev);
  424. if (ret)
  425. {
  426. pr_err("register platform device failed.\n");
  427. return ret;
  428. }
  429. #endif
  430. ret = platform_driver_register(&vvcam_sensor_driver);
  431. if (ret)
  432. {
  433. pr_err("register platform driver failed.\n");
  434. return ret;
  435. }
  436. return ret;
  437. }
  438. static void __exit vvcam_sensor_exit_module(void)
  439. {
  440. pr_debug("enter %s\n", __func__);
  441. platform_driver_unregister(&vvcam_sensor_driver);
  442. #if 0//def WITH_VVCAM
  443. platform_device_unregister(&vvcam_sensor_pdev);
  444. #endif
  445. #if 0//def WITH_VVCAM_DUAL
  446. platform_device_unregister(&vvcam_sensor_dual_pdev);
  447. #endif
  448. }
  449. module_init(vvcam_sensor_init_module);
  450. module_exit(vvcam_sensor_exit_module);
  451. MODULE_DESCRIPTION("SENSOR");
  452. MODULE_LICENSE("GPL");