flash_led_ioctl.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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 <linux/module.h>
  54. #include <linux/uaccess.h>
  55. #include <linux/i2c.h>
  56. #include <linux/slab.h>
  57. #include <linux/interrupt.h>
  58. #include <linux/cdev.h>
  59. #include <linux/debugfs.h>
  60. #include <linux/delay.h>
  61. #include <linux/gpio.h>
  62. #include <linux/io.h>
  63. #include <linux/kernel.h>
  64. #include <linux/module.h>
  65. #include <linux/of.h>
  66. #include <linux/of_address.h>
  67. #include <linux/of_gpio.h>
  68. #include <linux/platform_device.h>
  69. #include <linux/spinlock.h>
  70. #include "flash_led_ioctl.h"
  71. #include "flash_led_driver.h"
  72. void frame_irq_cnt_clear(struct flash_led_ctrl *pflash_led_dev);
  73. extern struct flash_led_function_s aw36515_function;
  74. extern struct flash_led_function_s aw36413_function;
  75. static struct flash_led_function_s *floodlight_func = &aw36413_function;
  76. static struct flash_led_function_s *projection_func = &aw36515_function;
  77. #define check_retval(x)\
  78. do {\
  79. if ((x))\
  80. return -EIO;\
  81. } while (0)
  82. /*
  83. static int32_t flash_led_sccb_config(struct flash_led_dev *dev, struct flash_led_sccb_cfg_s *sccb_config)
  84. {
  85. dev->flash_led_sccb_cfg.slave_addr = sccb_config->slave_addr;
  86. dev->flash_led_sccb_cfg.addr_byte = sccb_config->addr_byte;
  87. dev->flash_led_sccb_cfg.data_byte = sccb_config->data_byte;
  88. return 0;
  89. }
  90. */
  91. static int i2c_write_reg(struct i2c_client *client,unsigned int slave_address,
  92. unsigned int reg_addr,unsigned int reg_length,
  93. unsigned int data, unsigned int data_length)
  94. {
  95. int ret;
  96. unsigned int i;
  97. struct i2c_msg msgs[2];
  98. unsigned char sendbuf[16];
  99. unsigned int send_len = 0;
  100. if (client == NULL)
  101. return -1;
  102. memset(msgs,0,sizeof(msgs));
  103. memset(sendbuf,0,sizeof(sendbuf));
  104. for (i=0; i < reg_length; i++)
  105. {
  106. sendbuf[send_len++] = (reg_addr >> ((reg_length -1 - i)<<3)) & 0xff;
  107. }
  108. for (i=0; i < data_length; i++)
  109. {
  110. sendbuf[send_len++] = (data >> ((data_length -1 - i)<<3)) & 0xff;
  111. }
  112. msgs[0].addr = slave_address;
  113. msgs[0].flags = client->flags & I2C_M_TEN;
  114. msgs[0].len = send_len;
  115. msgs[0].buf = sendbuf;
  116. ret = i2c_transfer(client->adapter, msgs, 1);
  117. if (ret != 1)
  118. {
  119. return -1;
  120. }
  121. return 0;
  122. }
  123. static int i2c_read_reg(struct i2c_client *client,unsigned int slave_address,
  124. unsigned int reg_addr,unsigned int reg_length,
  125. unsigned char * pdata, unsigned int data_length)
  126. {
  127. int ret;
  128. unsigned int i;
  129. struct i2c_msg msgs[2];
  130. unsigned char sendbuf[16];
  131. unsigned int send_len = 0;
  132. unsigned char readbuf[16];
  133. if (client == NULL || pdata == NULL)
  134. return -1;
  135. memset(msgs,0,sizeof(msgs));
  136. memset(sendbuf,0,sizeof(sendbuf));
  137. memset(readbuf,0,sizeof(readbuf));
  138. for (i=0; i < reg_length; i++)
  139. {
  140. sendbuf[send_len++] = (reg_addr >> ((reg_length -1 - i)<<3)) & 0xff;
  141. }
  142. msgs[0].addr = slave_address;
  143. msgs[0].flags = client->flags & I2C_M_TEN;
  144. msgs[0].len = send_len;
  145. msgs[0].buf = sendbuf;
  146. msgs[1].addr = slave_address;
  147. msgs[1].flags = client->flags & I2C_M_TEN;
  148. msgs[1].flags |= I2C_M_RD;
  149. msgs[1].len = data_length;
  150. msgs[1].buf = readbuf;
  151. ret = i2c_transfer(client->adapter, msgs, 2);
  152. if (ret != 2) {
  153. return -1;
  154. }
  155. for (i = 0; i < data_length; i++) {
  156. pdata[i] = readbuf[data_length -1 - i];
  157. }
  158. return 0;
  159. }
  160. int32_t flash_led_i2c_write(struct flash_led_dev *dev, uint32_t address, uint32_t data)
  161. {
  162. int32_t ret = 0;
  163. if((NULL == dev))
  164. {
  165. return -1;
  166. }
  167. ret = i2c_write_reg(dev->i2c_client,dev->flash_led_sccb_cfg.slave_addr,
  168. address,dev->flash_led_sccb_cfg.addr_byte,
  169. data,dev->flash_led_sccb_cfg.data_byte);
  170. if (ret != 0) {
  171. printk("%s error\n", __func__);
  172. }
  173. return ret;
  174. }
  175. int32_t flash_led_i2c_read(struct flash_led_dev *dev, uint32_t address, uint32_t *pdata)
  176. {
  177. int32_t ret = 0;
  178. if((NULL == dev))
  179. {
  180. return -1;
  181. }
  182. ret = i2c_read_reg(dev->i2c_client,dev->flash_led_sccb_cfg.slave_addr,
  183. address, dev->flash_led_sccb_cfg.addr_byte,
  184. (unsigned char *)pdata, dev->flash_led_sccb_cfg.data_byte);
  185. return ret;
  186. }
  187. long flash_led_priv_ioctl(struct flash_led_ctrl *dev, unsigned int cmd, void __user *args)
  188. {
  189. int ret = -1;
  190. flash_bright_cfg_t bright;
  191. flash_led_reg_t reg;
  192. flash_ch_t ch;
  193. flash_mode_t mode;
  194. flash_led_enable_mask_t flash_led;
  195. struct flash_led_dev *floodlight = &dev->floodlight;
  196. struct flash_led_dev *projection = &dev->projection;
  197. if (!dev) {
  198. pr_err("-->%s: null point!\n", __func__);
  199. return ret;
  200. }
  201. switch (cmd) {
  202. case FLASH_LED_IOCTL_ENABLE_CH:
  203. check_retval(copy_from_user(&ch, args, sizeof(ch)));
  204. if (ch.type == FLOODLIGHT && floodlight->flash_led_func != NULL) {
  205. ret = floodlight->flash_led_func->enable_channel(floodlight, ch.channel);
  206. } else if (projection->flash_led_func != NULL) {
  207. ret = projection->flash_led_func->enable_channel(projection, ch.channel);
  208. }
  209. break;
  210. case FLASH_LED_IOCTL_DISABLE_CH:
  211. check_retval(copy_from_user(&ch, args, sizeof(ch)));
  212. if (ch.type == FLOODLIGHT && floodlight->flash_led_func != NULL) {
  213. ret = floodlight->flash_led_func->disable_channel(floodlight, ch.channel);
  214. } else if (projection->flash_led_func != NULL) {
  215. ret = projection->flash_led_func->disable_channel(projection, ch.channel);
  216. }
  217. break;
  218. case FLASH_LED_IOCTL_SET_MODE:
  219. check_retval(copy_from_user(&mode, args, sizeof(mode)));
  220. if (mode.type == FLOODLIGHT && floodlight->flash_led_func != NULL) {
  221. ret = floodlight->flash_led_func->set_mode(floodlight, mode.mode);
  222. } else if (projection->flash_led_func != NULL) {
  223. ret = projection->flash_led_func->set_mode(projection, mode.mode);
  224. }
  225. break;
  226. case FLASH_LED_IOCTL_SET_FLASH_BRIGHT:
  227. check_retval(copy_from_user(&bright, args, sizeof(bright)));
  228. if (bright.type == FLOODLIGHT && floodlight->flash_led_func != NULL) {
  229. ret = floodlight->flash_led_func->set_flash_brightness(floodlight, bright.channel, bright.value);
  230. } else if (projection->flash_led_func != NULL) {
  231. ret = projection->flash_led_func->set_flash_brightness(projection, bright.channel, bright.value);
  232. }
  233. break;
  234. case FLASH_LED_IOCTL_SET_TORCH_BRIGHT:
  235. check_retval(copy_from_user(&bright, args, sizeof(bright)));
  236. if (bright.type == FLOODLIGHT && floodlight->flash_led_func != NULL) {
  237. ret = floodlight->flash_led_func->set_torch_brightness(floodlight, bright.channel, bright.value);
  238. } else if (projection->flash_led_func != NULL) {
  239. ret = projection->flash_led_func->set_torch_brightness(projection, bright.channel, bright.value);
  240. }
  241. break;
  242. case FLASH_LED_IOCTL_WRITE_REG:
  243. check_retval(copy_from_user(&reg, args, sizeof(reg)));
  244. if (reg.type == FLOODLIGHT && floodlight->i2c_bus != UNDEFINED_IN_DTS) {
  245. ret = flash_led_i2c_write(floodlight, reg.offset, reg.value);
  246. } else if(projection->i2c_bus != UNDEFINED_IN_DTS) {
  247. ret = flash_led_i2c_write(projection, reg.offset, reg.value);
  248. }
  249. break;
  250. case FLASH_LED_IOCTL_READ_REG:
  251. check_retval(copy_from_user(&reg, args, sizeof(reg)));
  252. if (reg.type == FLOODLIGHT && floodlight->i2c_bus != UNDEFINED_IN_DTS) {
  253. ret = flash_led_i2c_read(floodlight, reg.offset, &reg.value);
  254. } else if(projection->i2c_bus != UNDEFINED_IN_DTS) {
  255. ret = flash_led_i2c_read(projection, reg.offset, &reg.value);
  256. }
  257. check_retval(copy_to_user(args, &reg, sizeof(reg)));
  258. break;
  259. case FLASH_LED_IOCTL_ENABLE:
  260. check_retval(copy_from_user(&flash_led, args, sizeof(flash_led)));
  261. frame_irq_cnt_clear(dev);
  262. dev->enable |= flash_led;
  263. flash_led_switch(dev,0);
  264. ret = 0;
  265. break;
  266. case FLASH_LED_IOCTL_DISABLE:
  267. check_retval(copy_from_user(&flash_led, args, sizeof(flash_led)));
  268. dev->enable &= ~flash_led;
  269. if (flash_led & FLOODLIGHT_EN && floodlight->flash_led_func != NULL) {
  270. floodlight->flash_led_func->disable_channel(floodlight, 3);
  271. }
  272. if (flash_led & PROJECTION_EN && projection->flash_led_func != NULL) {
  273. projection->flash_led_func->disable_channel(projection, 3);
  274. }
  275. ret = 0;
  276. break;
  277. case FLASH_LED_IOCTL_GET_FRAME_MASK_INFO_ADDR: {
  278. unsigned long addr;
  279. addr = dev->frame_mark_info_addr;
  280. pr_info("FLASH_LED_IOCTL_GET_FRAME_MASK_INFO_ADDR 0x%lx\n", addr);
  281. check_retval(copy_to_user(args, &addr, sizeof(addr)));
  282. ret = 0;
  283. }
  284. break;
  285. case FLASH_LED_IOCTL_SET_SWITCH_MODE: {
  286. check_retval(copy_from_user(&dev->switch_mode,args, sizeof(dev->switch_mode)));
  287. pr_info("FLASH_LED_IOCTL_SET_SWITCH_MODE 0x%lx\n", dev->switch_mode);
  288. ret = 0;
  289. }
  290. break;
  291. default:
  292. printk("%s, %d, flash led cmd error, cmd %u\n", __func__, __LINE__, cmd);
  293. return -ENOTTY;
  294. }
  295. return ret;
  296. }
  297. static int register_i2c_client(struct flash_led_dev *dev)
  298. {
  299. struct i2c_adapter *adap;
  300. struct i2c_board_info flash_led_i2c_info = {
  301. .type = "flash_led",
  302. .addr = dev->flash_led_sccb_cfg.slave_addr,
  303. };
  304. if (dev->i2c_bus == UNDEFINED_IN_DTS) {
  305. return 0;
  306. }
  307. flash_led_i2c_info.addr &= 0xff;
  308. adap = i2c_get_adapter(dev->i2c_bus);
  309. if (adap == NULL)
  310. {
  311. pr_err("[%s]:i2c_get_adapter i2c_bus %d failed\n",__func__,dev->i2c_bus);
  312. return -1;
  313. }
  314. strscpy(flash_led_i2c_info.type, dev->flash_led_func->flash_led_name, I2C_NAME_SIZE);
  315. dev->i2c_client = i2c_new_client_device(adap, &flash_led_i2c_info);
  316. i2c_put_adapter(adap);
  317. if (dev->i2c_client == NULL) {
  318. pr_err("[%s]:i2c_new_client_device i2c_bus %d failed\n",__func__,dev->i2c_bus);
  319. return -1;
  320. }
  321. return 0;
  322. }
  323. static void unregister_i2c_client(struct flash_led_dev *dev)
  324. {
  325. i2c_unregister_device(dev->i2c_client);
  326. }
  327. int flash_led_init(struct flash_led_ctrl *dev)
  328. {
  329. int ret = 0;
  330. struct flash_led_dev *floodlight = &dev->floodlight;
  331. struct flash_led_dev *projection = &dev->projection;
  332. floodlight->flash_led_sccb_cfg.slave_addr = dev->device_idx+1;
  333. projection->flash_led_sccb_cfg.slave_addr = dev->device_idx+2;
  334. floodlight->flash_led_func = NULL;
  335. projection->flash_led_func = NULL;
  336. if (floodlight->i2c_bus == UNDEFINED_IN_DTS && projection->i2c_bus == UNDEFINED_IN_DTS) {
  337. pr_warn("%s, %d, flash_led i2c bus invalid\n",__func__, __LINE__);
  338. return -1;
  339. }
  340. dev->switch_mode = BOTH_OFF;
  341. if (floodlight->i2c_bus != UNDEFINED_IN_DTS) {
  342. floodlight->flash_led_func = floodlight_func;
  343. dev->switch_mode = FLOODLIGHT_ALWAYS_ON;
  344. }
  345. if (projection->i2c_bus != UNDEFINED_IN_DTS) {
  346. projection->flash_led_func = projection_func;
  347. dev->switch_mode = dev->switch_mode == FLOODLIGHT_ALWAYS_ON ?
  348. PROJECTION_EVEN_FLOODLIGHT_ODD:PROJECTION_ALWAYS_ON;
  349. }
  350. if (floodlight->i2c_bus == projection->i2c_bus) {
  351. ret = register_i2c_client(floodlight);
  352. if (ret != 0) {
  353. pr_err("[%s]: register_i2c_client flash_led_idx = %d failed\n",__func__, dev->device_idx);
  354. return -1;
  355. }
  356. projection->i2c_client = floodlight->i2c_client;
  357. } else {
  358. if (floodlight->i2c_bus != UNDEFINED_IN_DTS) {
  359. ret = register_i2c_client(floodlight);
  360. if (ret != 0) {
  361. pr_err("[%s]: floodlight register_i2c_client flash_led_idx = %d failed\n",__func__, dev->device_idx);
  362. return -1;
  363. }
  364. }
  365. if (projection->i2c_bus != UNDEFINED_IN_DTS) {
  366. ret = register_i2c_client(projection);
  367. if (ret != 0) {
  368. pr_err("[%s]: projection register_i2c_client flash_led_idx = %d failed\n",__func__, dev->device_idx);
  369. return -1;
  370. }
  371. }
  372. }
  373. pr_info("%s,%d, exit\n", __func__, __LINE__);
  374. return ret;
  375. }
  376. int flash_led_deinit(struct flash_led_ctrl *dev)
  377. {
  378. int ret = 0, err = 0;
  379. struct flash_led_dev *floodlight = &dev->floodlight;
  380. struct flash_led_dev *projection = &dev->projection;
  381. if (floodlight->flash_led_func != NULL) {
  382. ret = floodlight->flash_led_func->uninit(floodlight);
  383. if (ret != 0) {
  384. err |= ret;
  385. }
  386. }
  387. if (projection->flash_led_func != NULL) {
  388. ret = projection->flash_led_func->uninit(projection);
  389. if (ret != 0) {
  390. err |= ret;
  391. }
  392. }
  393. if (floodlight->i2c_bus == projection->i2c_bus) {
  394. unregister_i2c_client(floodlight);
  395. } else {
  396. if (floodlight->i2c_bus != UNDEFINED_IN_DTS) {
  397. unregister_i2c_client(floodlight);
  398. }
  399. if (projection->i2c_bus != UNDEFINED_IN_DTS) {
  400. unregister_i2c_client(projection);
  401. }
  402. }
  403. return err;
  404. }