leds-max77803.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. /*
  2. * LED driver for Maxim MAX77803 - leds-max77803.c
  3. *
  4. * Copyright (C) 2013 Samsung co. Ltd.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/leds.h>
  14. #include <linux/workqueue.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/slab.h>
  17. #include <linux/mfd/max77803.h>
  18. #include <linux/mfd/max77803-private.h>
  19. #include <linux/leds-max77803.h>
  20. #include <linux/ctype.h>
  21. #include <linux/gpio.h>
  22. #ifdef CONFIG_LEDS_SWITCH
  23. #include <linux/gpio.h>
  24. #define FLASH_SWITCH_REMOVED_REVISION 0x05
  25. #endif
  26. #undef DEBUG_FLASH
  27. //#define DEBUG_FLASH
  28. #if defined(DEBUG_FLASH)
  29. #define DEBUG_MAX77803(fmt, args...) pr_err(fmt, ##args)
  30. #else
  31. #define DEBUG_MAX77803(fmt, args...) do{}while(0)
  32. #endif
  33. struct max77803_led_data {
  34. struct led_classdev led;
  35. struct max77803_dev *max77803;
  36. struct max77803_led *data;
  37. struct i2c_client *i2c;
  38. struct work_struct work;
  39. struct mutex lock;
  40. spinlock_t value_lock;
  41. int brightness;
  42. int test_brightness;
  43. #ifdef CONFIG_LEDS_SEPERATE_MOVIE_FLASH
  44. int movie_brightness;
  45. #endif
  46. };
  47. static u8 led_en_mask[MAX77803_LED_MAX] = {
  48. MAX77803_FLASH_FLED1_EN,
  49. MAX77803_TORCH_FLED1_EN,
  50. };
  51. static u8 led_en_shift[MAX77803_LED_MAX] = {
  52. 6,
  53. 2,
  54. };
  55. static u8 reg_led_timer[MAX77803_LED_MAX] = {
  56. MAX77803_LED_REG_FLASH_TIMER,
  57. MAX77803_LED_REG_ITORCHTORCHTIMER,
  58. };
  59. static u8 reg_led_current[MAX77803_LED_MAX] = {
  60. MAX77803_LED_REG_IFLASH,
  61. MAX77803_LED_REG_ITORCH,
  62. };
  63. static u8 led_current_mask[MAX77803_LED_MAX] = {
  64. MAX77803_FLASH_IOUT,
  65. MAX77803_TORCH_IOUT,
  66. };
  67. static u8 led_current_shift[MAX77803_LED_MAX] = {
  68. 0,
  69. 0,
  70. };
  71. extern struct class *camera_class; /*sys/class/camera*/
  72. struct device *flash_dev;
  73. static int flash_torch_en;
  74. extern int led_torch_en;
  75. extern int led_flash_en;
  76. static int max77803_set_bits(struct i2c_client *client, const u8 reg,
  77. const u8 mask, const u8 inval)
  78. {
  79. int ret;
  80. u8 value;
  81. ret = max77803_read_reg(client, reg, &value);
  82. if (unlikely(ret < 0))
  83. return ret;
  84. value = (value & ~mask) | (inval & mask);
  85. ret = max77803_write_reg(client, reg, value);
  86. return ret;
  87. }
  88. #ifdef CONFIG_LEDS_SEPERATE_MOVIE_FLASH
  89. static ssize_t max77803_show_movie_brightness(struct device *dev,
  90. struct device_attribute *devattr, char *buf)
  91. {
  92. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  93. struct max77803_led_data *led_data = container_of(led_cdev, struct max77803_led_data, led);
  94. return sprintf(buf, "%d\n", led_data->movie_brightness);
  95. }
  96. static ssize_t max77803_store_movie_brightness(struct device *dev,
  97. struct device_attribute *attr, const char *buf, size_t size)
  98. {
  99. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  100. struct max77803_led_data *led_data = container_of(led_cdev, struct max77803_led_data, led);
  101. unsigned long movie_brightness = simple_strtoul(buf, NULL, 0);
  102. int set_brightness, ret;
  103. u8 shift = led_current_shift[led_data->data->id];
  104. if (movie_brightness > 0) {
  105. led_data->movie_brightness = (int)movie_brightness;
  106. set_brightness = led_data->movie_brightness;
  107. } else {
  108. led_data->movie_brightness = 0;
  109. set_brightness = led_data->brightness;
  110. }
  111. pr_debug("[LED] %s:movie_brightness = %d\n", __func__, led_data->movie_brightness);
  112. ret = max77803_set_bits(led_data->i2c, reg_led_current[led_data->data->id],
  113. led_current_mask[led_data->data->id],
  114. set_brightness << shift);
  115. if (unlikely(ret))
  116. pr_err("%s: can't set led level %d\n", __func__, ret);
  117. return size;
  118. }
  119. static DEVICE_ATTR(movie_brightness, S_IWUSR|S_IWGRP|S_IRUGO,
  120. max77803_show_movie_brightness, max77803_store_movie_brightness);
  121. #endif
  122. /*
  123. static void print_all_reg_value(struct i2c_client *client)
  124. {
  125. u8 value;
  126. u8 i;
  127. for (i = 0; i != 0x11; ++i) {
  128. max77803_read_reg(client, i, &value);
  129. printk(KERN_ERR "LEDS_MAX77803 REG(%d) = %x\n", i, value);
  130. }
  131. }
  132. */
  133. static int max77803_led_get_en_value(struct max77803_led_data *led_data, int on)
  134. {
  135. int mode;
  136. int ret;
  137. if (on)
  138. mode = 0x03; /*triggered via serial interface*/
  139. else {
  140. if (reg_led_timer[led_data->data->id]
  141. == MAX77803_LED_REG_FLASH_TIMER)
  142. mode = 0x01; /*Flash triggered via FLASHEN*/
  143. else
  144. mode = 0x02; /*Torch triggered via TORCHEN*/
  145. if (flash_torch_en) {
  146. if (mode == 0x01) {
  147. ret = gpio_request(led_flash_en, "max77803_flash_en");
  148. if (ret)
  149. pr_err("can't get max77803_flash_en");
  150. else {
  151. DEBUG_MAX77803("[LED] %s: max77803_flash_en set 0\n", __func__);
  152. gpio_direction_output(led_flash_en, 0);
  153. gpio_free(led_flash_en);
  154. }
  155. }
  156. if (mode == 0x02) {
  157. ret = gpio_request(led_torch_en, "max77803_torch_en");
  158. if (ret)
  159. pr_err("can't get max77803_torch_en");
  160. else {
  161. DEBUG_MAX77803("[LED] %s: max77803_torch_en set 0\n", __func__);
  162. gpio_direction_output(led_torch_en, 0);
  163. gpio_free(led_torch_en);
  164. }
  165. }
  166. } else
  167. pr_err("%s : can't find gpio", __func__);
  168. }
  169. DEBUG_MAX77803("[LED] %s: led mode: 0x%x, on: %d\n", __func__, mode, on);
  170. return mode;
  171. }
  172. int max77803_led_en(int onoff, int mode)
  173. {
  174. int ret = 0;
  175. if (flash_torch_en) {
  176. if (onoff) { /* enable */
  177. if (mode) { /* flash */
  178. DEBUG_MAX77803("[LED] %s: max77803_flash_en set 1\n", __func__);
  179. gpio_direction_output(led_flash_en, 1);
  180. } else { /* torch */
  181. DEBUG_MAX77803("[LED] %s: max77803_torch_en set 1\n", __func__);
  182. gpio_direction_output(led_torch_en, 1);
  183. }
  184. } else { /* disable */
  185. if (mode) { /* flash */
  186. DEBUG_MAX77803("[LED] %s: max77803_flash_en set 0\n", __func__);
  187. gpio_direction_output(led_flash_en, 0);
  188. } else { /* torch */
  189. DEBUG_MAX77803("[LED] %s: max77803_torch_en set 0\n", __func__);
  190. gpio_direction_output(led_torch_en, 0);
  191. }
  192. }
  193. } else {
  194. pr_err("%s : Error!!, find gpio", __func__);
  195. ret = -EINVAL;
  196. }
  197. return ret;
  198. }
  199. EXPORT_SYMBOL(max77803_led_en);
  200. static void max77803_led_set(struct led_classdev *led_cdev,
  201. enum led_brightness value)
  202. {
  203. unsigned long flags;
  204. struct max77803_led_data *led_data
  205. = container_of(led_cdev, struct max77803_led_data, led);
  206. DEBUG_MAX77803("[LED] %s\n", __func__);
  207. spin_lock_irqsave(&led_data->value_lock, flags);
  208. led_data->test_brightness = min((int)value, MAX77803_FLASH_IOUT);
  209. spin_unlock_irqrestore(&led_data->value_lock, flags);
  210. schedule_work(&led_data->work);
  211. }
  212. static void led_set(struct max77803_led_data *led_data)
  213. {
  214. int ret;
  215. struct max77803_led *data = led_data->data;
  216. int id = data->id;
  217. u8 shift = led_current_shift[id];
  218. int value;
  219. if (led_data->test_brightness == LED_OFF) {
  220. DEBUG_MAX77803("[LED] %s : LED_OFF\n", __func__);
  221. value = max77803_led_get_en_value(led_data, 0);
  222. ret = max77803_set_bits(led_data->i2c,
  223. MAX77803_LED_REG_FLASH_EN,
  224. led_en_mask[id],
  225. value << led_en_shift[id]);
  226. if (unlikely(ret))
  227. goto error_set_bits;
  228. ret = max77803_set_bits(led_data->i2c, reg_led_current[id],
  229. led_current_mask[id],
  230. led_data->brightness << shift);
  231. if (unlikely(ret))
  232. goto error_set_bits;
  233. return;
  234. }
  235. /* Set current */
  236. ret = max77803_set_bits(led_data->i2c, reg_led_current[id],
  237. led_current_mask[id],
  238. led_data->test_brightness << shift);
  239. if (unlikely(ret))
  240. goto error_set_bits;
  241. /* Turn on LED */
  242. DEBUG_MAX77803("[LED] %s : LED turns on\n", __func__);
  243. value = max77803_led_get_en_value(led_data, 1);
  244. ret = max77803_set_bits(led_data->i2c, MAX77803_LED_REG_FLASH_EN,
  245. led_en_mask[id],
  246. value << led_en_shift[id]);
  247. if (unlikely(ret))
  248. goto error_set_bits;
  249. return;
  250. error_set_bits:
  251. pr_err("%s: can't set led level %d\n", __func__, ret);
  252. return;
  253. }
  254. static void max77803_led_work(struct work_struct *work)
  255. {
  256. struct max77803_led_data *led_data
  257. = container_of(work, struct max77803_led_data, work);
  258. DEBUG_MAX77803("[LED] %s\n", __func__);
  259. mutex_lock(&led_data->lock);
  260. led_set(led_data);
  261. mutex_unlock(&led_data->lock);
  262. }
  263. static int max77803_led_setup(struct max77803_led_data *led_data)
  264. {
  265. int ret = 0;
  266. struct max77803_led *data = led_data->data;
  267. int id = data->id;
  268. int value;
  269. DEBUG_MAX77803("[LED] %s\n", __func__);
  270. ret |= max77803_write_reg(led_data->i2c, MAX77803_LED_REG_VOUT_CNTL,
  271. MAX77803_BOOST_FLASH_MODE_FLED1);
  272. ret |= max77803_write_reg(led_data->i2c, MAX77803_LED_REG_VOUT_FLASH,
  273. MAX77803_BOOST_VOUT_FLASH_FROM_VOLT(3300));
  274. ret |= max77803_write_reg(led_data->i2c, MAX77803_CHG_REG_CHG_CNFG_11, 0x2B);
  275. ret |= max77803_write_reg(led_data->i2c,
  276. MAX77803_LED_REG_MAX_FLASH1, 0xBC);
  277. ret |= max77803_write_reg(led_data->i2c,
  278. MAX77803_LED_REG_MAX_FLASH2, 0x00);
  279. value = max77803_led_get_en_value(led_data, 0);
  280. ret |= max77803_set_bits(led_data->i2c, MAX77803_LED_REG_FLASH_EN,
  281. led_en_mask[id],
  282. value << led_en_shift[id]);
  283. /* Set TORCH_TMR_DUR or FLASH_TMR_DUR */
  284. if (reg_led_timer[id] == MAX77803_LED_REG_FLASH_TIMER) {
  285. ret |= max77803_write_reg(led_data->i2c, reg_led_timer[id],
  286. (data->timer | data->timer_mode << 7));
  287. } else {
  288. ret |= max77803_write_reg(led_data->i2c, reg_led_timer[id],
  289. 0xC0);
  290. }
  291. /* Set current */
  292. ret |= max77803_set_bits(led_data->i2c, reg_led_current[id],
  293. led_current_mask[id],
  294. led_data->brightness << led_current_shift[id]);
  295. return ret;
  296. }
  297. static ssize_t max77803_flash(struct device *dev,
  298. struct device_attribute *attr, const char *buf, size_t size)
  299. {
  300. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  301. ssize_t ret = -EINVAL;
  302. char *after;
  303. unsigned long state = simple_strtoul(buf, &after, 10);
  304. size_t count = after - buf;
  305. DEBUG_MAX77803("[LED] %s\n", __func__);
  306. if (isspace(*after))
  307. count++;
  308. if (count == size) {
  309. ret = count;
  310. if (state > led_cdev->max_brightness)
  311. state = led_cdev->max_brightness;
  312. led_cdev->brightness = state;
  313. pr_warn("[LED] %s : led_cdev->brightness %d\n",
  314. __func__, led_cdev->brightness);
  315. if (!(led_cdev->flags & LED_SUSPENDED))
  316. led_cdev->brightness_set(led_cdev, state);
  317. }
  318. return ret;
  319. }
  320. static DEVICE_ATTR(rear_flash, S_IWUSR|S_IWGRP|S_IROTH,
  321. NULL, max77803_flash);
  322. static int max77803_led_probe(struct platform_device *pdev)
  323. {
  324. int ret = 0;
  325. int i;
  326. struct max77803_dev *max77803 = dev_get_drvdata(pdev->dev.parent);
  327. struct max77803_platform_data *max77803_pdata
  328. = dev_get_platdata(max77803->dev);
  329. struct max77803_led_platform_data *pdata = max77803_pdata->led_data;
  330. struct max77803_led_data *led_data;
  331. struct max77803_led *data;
  332. struct max77803_led_data **led_datas;
  333. if (pdata == NULL) {
  334. pr_err("[LED] no platform data for this led is found\n");
  335. return -EFAULT;
  336. }
  337. led_datas = kzalloc(sizeof(struct max77803_led_data *)
  338. * MAX77803_LED_MAX, GFP_KERNEL);
  339. if (unlikely(!led_datas)) {
  340. pr_err("[LED] memory allocation error %s", __func__);
  341. return -ENOMEM;
  342. }
  343. platform_set_drvdata(pdev, led_datas);
  344. if (led_torch_en && led_flash_en) {
  345. DEBUG_MAX77803("[LED] %s, led_torch %d, led_flash %d\n", __func__,
  346. led_torch_en, led_flash_en);
  347. flash_torch_en = 1;
  348. } else {
  349. pr_err("%s : can't find gpio", __func__);
  350. flash_torch_en = 0;
  351. }
  352. for (i = 0; i != pdata->num_leds; ++i) {
  353. data = &(pdata->leds[i]);
  354. led_data = kzalloc(sizeof(struct max77803_led_data),
  355. GFP_KERNEL);
  356. led_datas[i] = led_data;
  357. if (unlikely(!led_data)) {
  358. pr_err("[LED] memory allocation error %s\n", __func__);
  359. ret = -ENOMEM;
  360. continue;
  361. }
  362. led_data->max77803 = max77803;
  363. led_data->i2c = max77803->i2c;
  364. led_data->data = data;
  365. led_data->led.name = data->name;
  366. led_data->led.brightness_set = max77803_led_set;
  367. led_data->led.brightness = LED_OFF;
  368. led_data->brightness = data->brightness;
  369. led_data->led.flags = 0;
  370. led_data->led.max_brightness = reg_led_timer[data->id] == MAX77803_LED_REG_FLASH_TIMER
  371. ? MAX_FLASH_DRV_LEVEL : MAX_TORCH_DRV_LEVEL;
  372. mutex_init(&led_data->lock);
  373. spin_lock_init(&led_data->value_lock);
  374. INIT_WORK(&led_data->work, max77803_led_work);
  375. ret = led_classdev_register(&pdev->dev, &led_data->led);
  376. if (unlikely(ret)) {
  377. pr_err("unable to register LED\n");
  378. kfree(led_data);
  379. ret = -EFAULT;
  380. continue;
  381. }
  382. ret = max77803_led_setup(led_data);
  383. if (unlikely(ret)) {
  384. pr_err("unable to register LED\n");
  385. mutex_destroy(&led_data->lock);
  386. led_classdev_unregister(&led_data->led);
  387. kfree(led_data);
  388. ret = -EFAULT;
  389. }
  390. }
  391. /* print_all_reg_value(max77803->i2c); */
  392. if (!IS_ERR(camera_class)) {
  393. flash_dev = device_create(camera_class, NULL, 0, led_datas[1], "flash");
  394. if (flash_dev < 0)
  395. pr_err("Failed to create device(flash)!\n");
  396. if (device_create_file(flash_dev, &dev_attr_rear_flash) < 0) {
  397. pr_err("failed to create device file, %s\n",
  398. dev_attr_rear_flash.attr.name);
  399. }
  400. #ifdef CONFIG_LEDS_SEPERATE_MOVIE_FLASH
  401. if (device_create_file(flash_dev, &dev_attr_movie_brightness) < 0) {
  402. pr_err("failed to create device file, %s\n",
  403. dev_attr_movie_brightness.attr.name);
  404. }
  405. #endif
  406. } else
  407. pr_err("Failed to create device(flash) because of nothing camera class!\n");
  408. #ifdef CONFIG_LEDS_SWITCH
  409. if (system_rev < FLASH_SWITCH_REMOVED_REVISION) {
  410. if (gpio_request(GPIO_CAM_SW_EN, "CAM_SW_EN"))
  411. pr_err("failed to request CAM_SW_EN\n");
  412. else
  413. gpio_direction_output(GPIO_CAM_SW_EN, 1);
  414. }
  415. #endif
  416. return ret;
  417. }
  418. static int __devexit max77803_led_remove(struct platform_device *pdev)
  419. {
  420. struct max77803_led_data **led_datas = platform_get_drvdata(pdev);
  421. int i;
  422. for (i = 0; i != MAX77803_LED_MAX; ++i) {
  423. if (led_datas[i] == NULL)
  424. continue;
  425. cancel_work_sync(&led_datas[i]->work);
  426. mutex_destroy(&led_datas[i]->lock);
  427. led_classdev_unregister(&led_datas[i]->led);
  428. kfree(led_datas[i]);
  429. }
  430. kfree(led_datas);
  431. device_remove_file(flash_dev, &dev_attr_rear_flash);
  432. #ifdef CONFIG_LEDS_SEPERATE_MOVIE_FLASH
  433. device_remove_file(flash_dev, &dev_attr_movie_brightness);
  434. #endif
  435. device_destroy(camera_class, 0);
  436. class_destroy(camera_class);
  437. return 0;
  438. }
  439. void max77803_led_shutdown(struct device *dev)
  440. {
  441. struct max77803_led_data **led_datas = dev_get_drvdata(dev);
  442. /* Turn off LED */
  443. max77803_set_bits(led_datas[1]->i2c,
  444. MAX77803_LED_REG_FLASH_EN,
  445. led_en_mask[1],
  446. 0x02 << led_en_shift[1]);
  447. }
  448. static struct platform_driver max77803_led_driver = {
  449. .probe = max77803_led_probe,
  450. .remove = __devexit_p(max77803_led_remove),
  451. .driver = {
  452. .name = "max77803-led",
  453. .owner = THIS_MODULE,
  454. .shutdown = max77803_led_shutdown,
  455. },
  456. };
  457. static int __init max77803_led_init(void)
  458. {
  459. return platform_driver_register(&max77803_led_driver);
  460. }
  461. module_init(max77803_led_init);
  462. static void __exit max77803_led_exit(void)
  463. {
  464. platform_driver_unregister(&max77803_led_driver);
  465. }
  466. module_exit(max77803_led_exit);
  467. MODULE_AUTHOR("Samsung co. Ltd.");
  468. MODULE_DESCRIPTION("MAX77803 LED driver");
  469. MODULE_LICENSE("GPL");