leds-max77888.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. /*
  2. * LED driver for Maxim MAX77888 - leds-max77888.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/max77888.h>
  18. #include <linux/mfd/max77888-private.h>
  19. #include <linux/leds-max77888.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_MAX77888(fmt, args...) pr_err(fmt, ##args)
  30. #else
  31. #define DEBUG_MAX77888(fmt, args...) do{}while(0)
  32. #endif
  33. struct max77888_led_data {
  34. struct led_classdev led;
  35. struct max77888_dev *max77888;
  36. struct max77888_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[MAX77888_LED_MAX] = {
  48. MAX77888_FLASH_FLED1_EN,
  49. MAX77888_TORCH_FLED1_EN,
  50. };
  51. static u8 led_en_shift[MAX77888_LED_MAX] = {
  52. 6,
  53. 2,
  54. };
  55. static u8 reg_led_timer[MAX77888_LED_MAX] = {
  56. MAX77888_LED_REG_FLASH_TIMER,
  57. MAX77888_LED_REG_ITORCHTORCHTIMER,
  58. };
  59. static u8 reg_led_current[MAX77888_LED_MAX] = {
  60. MAX77888_LED_REG_IFLASH,
  61. MAX77888_LED_REG_ITORCH,
  62. };
  63. static u8 led_current_mask[MAX77888_LED_MAX] = {
  64. MAX77888_FLASH_IOUT,
  65. MAX77888_TORCH_IOUT,
  66. };
  67. static u8 led_current_shift[MAX77888_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 max77888_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 = max77888_read_reg(client, reg, &value);
  82. if (unlikely(ret < 0))
  83. return ret;
  84. value = (value & ~mask) | (inval & mask);
  85. ret = max77888_write_reg(client, reg, value);
  86. return ret;
  87. }
  88. #ifdef CONFIG_LEDS_SEPERATE_MOVIE_FLASH
  89. static ssize_t max77888_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 max77888_led_data *led_data = container_of(led_cdev, struct max77888_led_data, led);
  94. return sprintf(buf, "%d\n", led_data->movie_brightness);
  95. }
  96. static ssize_t max77888_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 max77888_led_data *led_data = container_of(led_cdev, struct max77888_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 = max77888_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. max77888_show_movie_brightness, max77888_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. max77888_read_reg(client, i, &value);
  129. printk(KERN_ERR "LEDS_MAX77888 REG(%d) = %x\n", i, value);
  130. }
  131. }
  132. */
  133. static int max77888_led_get_en_value(struct max77888_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. == MAX77888_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, "max77888_flash_en");
  148. if (ret)
  149. pr_err("can't get max77888_flash_en");
  150. else {
  151. DEBUG_MAX77888("[LED] %s: max77888_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, "max77888_torch_en");
  158. if (ret)
  159. pr_err("can't get max77888_torch_en");
  160. else {
  161. DEBUG_MAX77888("[LED] %s: max77888_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_MAX77888("[LED] %s: led mode: 0x%x, on: %d\n", __func__, mode, on);
  170. return mode;
  171. }
  172. int max77888_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_MAX77888("[LED] %s: max77888_flash_en set 1\n", __func__);
  179. gpio_direction_output(led_flash_en, 1);
  180. } else { /* torch */
  181. DEBUG_MAX77888("[LED] %s: max77888_torch_en set 1\n", __func__);
  182. gpio_direction_output(led_torch_en, 1);
  183. }
  184. } else { /* disable */
  185. if (mode) { /* flash */
  186. DEBUG_MAX77888("[LED] %s: max77888_flash_en set 0\n", __func__);
  187. gpio_direction_output(led_flash_en, 0);
  188. } else { /* torch */
  189. DEBUG_MAX77888("[LED] %s: max77888_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(max77888_led_en);
  200. static void max77888_led_set(struct led_classdev *led_cdev,
  201. enum led_brightness value)
  202. {
  203. unsigned long flags;
  204. struct max77888_led_data *led_data
  205. = container_of(led_cdev, struct max77888_led_data, led);
  206. DEBUG_MAX77888("[LED] %s\n", __func__);
  207. spin_lock_irqsave(&led_data->value_lock, flags);
  208. led_data->test_brightness = min((int)value, MAX77888_FLASH_IOUT);
  209. spin_unlock_irqrestore(&led_data->value_lock, flags);
  210. schedule_work(&led_data->work);
  211. }
  212. static void led_set(struct max77888_led_data *led_data)
  213. {
  214. int ret;
  215. struct max77888_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_MAX77888("[LED] %s : LED_OFF\n", __func__);
  221. /* change MUIC Control3 to 'Auto Detection' */
  222. ret = max77888_muic_set_jigset(0x00);
  223. if (unlikely(ret))
  224. pr_err("[LED] %s : MUIC 0x0E write failed 0x00 \n", __func__);
  225. value = max77888_led_get_en_value(led_data, 0);
  226. ret = max77888_set_bits(led_data->i2c,
  227. MAX77888_LED_REG_FLASH_EN,
  228. led_en_mask[id],
  229. value << led_en_shift[id]);
  230. if (unlikely(ret))
  231. goto error_set_bits;
  232. ret = max77888_set_bits(led_data->i2c, reg_led_current[id],
  233. led_current_mask[id],
  234. led_data->brightness << shift);
  235. if (unlikely(ret))
  236. goto error_set_bits;
  237. } else {
  238. /* It is for Flash Control by UART, Max77888 have internal FET structure limitation */
  239. /* change MUIC Control3 to 'JIG pin Hi-Impedance' */
  240. ret = max77888_muic_set_jigset(0x03);
  241. if (unlikely(ret))
  242. pr_err("[LED] %s : MUIC 0x0E write failed 0x03 \n", __func__);
  243. /* Set current */
  244. ret = max77888_set_bits(led_data->i2c, reg_led_current[id],
  245. led_current_mask[id],
  246. led_data->test_brightness << shift);
  247. if (unlikely(ret))
  248. goto error_set_bits;
  249. /* Turn on LED */
  250. DEBUG_MAX77888("[LED] %s : LED turns on\n", __func__);
  251. value = max77888_led_get_en_value(led_data, 1);
  252. ret = max77888_set_bits(led_data->i2c, MAX77888_LED_REG_FLASH_EN,
  253. led_en_mask[id],
  254. value << led_en_shift[id]);
  255. if (unlikely(ret))
  256. goto error_set_bits;
  257. }
  258. return;
  259. error_set_bits:
  260. pr_err("%s: can't set led level %d\n", __func__, ret);
  261. /* change MUIC Control3 to 'Auto Detection' */
  262. ret = max77888_muic_set_jigset(0x00);
  263. if (unlikely(ret))
  264. pr_err("[LED] %s : MUIC 0x0E write failed 0x00 \n", __func__);
  265. return;
  266. }
  267. static void max77888_led_work(struct work_struct *work)
  268. {
  269. struct max77888_led_data *led_data
  270. = container_of(work, struct max77888_led_data, work);
  271. DEBUG_MAX77888("[LED] %s\n", __func__);
  272. mutex_lock(&led_data->lock);
  273. led_set(led_data);
  274. mutex_unlock(&led_data->lock);
  275. }
  276. static int max77888_led_setup(struct max77888_led_data *led_data)
  277. {
  278. int ret = 0;
  279. struct max77888_led *data = led_data->data;
  280. int id = data->id;
  281. int value;
  282. DEBUG_MAX77888("[LED] %s\n", __func__);
  283. ret |= max77888_write_reg(led_data->i2c, MAX77888_LED_REG_VOUT_CNTL,
  284. MAX77888_BOOST_FLASH_MODE_FLED1);
  285. ret |= max77888_write_reg(led_data->i2c, MAX77888_LED_REG_VOUT_FLASH,
  286. MAX77888_BOOST_VOUT_FLASH_FROM_VOLT(3300));
  287. ret |= max77888_write_reg(led_data->i2c, MAX77888_CHG_REG_CHG_CNFG_11, 0x2B);
  288. ret |= max77888_write_reg(led_data->i2c,
  289. MAX77888_LED_REG_MAX_FLASH1, 0xBC);
  290. ret |= max77888_write_reg(led_data->i2c,
  291. MAX77888_LED_REG_MAX_FLASH2, 0x00);
  292. value = max77888_led_get_en_value(led_data, 0);
  293. ret |= max77888_set_bits(led_data->i2c, MAX77888_LED_REG_FLASH_EN,
  294. led_en_mask[id],
  295. value << led_en_shift[id]);
  296. /* Set TORCH_TMR_DUR or FLASH_TMR_DUR */
  297. if (reg_led_timer[id] == MAX77888_LED_REG_FLASH_TIMER) {
  298. ret |= max77888_write_reg(led_data->i2c, reg_led_timer[id],
  299. (data->timer | data->timer_mode << 7));
  300. } else {
  301. ret |= max77888_write_reg(led_data->i2c, reg_led_timer[id],
  302. 0xC0);
  303. }
  304. /* Set current */
  305. ret |= max77888_set_bits(led_data->i2c, reg_led_current[id],
  306. led_current_mask[id],
  307. led_data->brightness << led_current_shift[id]);
  308. return ret;
  309. }
  310. static ssize_t max77888_flash(struct device *dev,
  311. struct device_attribute *attr, const char *buf, size_t size)
  312. {
  313. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  314. ssize_t ret = -EINVAL;
  315. char *after;
  316. unsigned long state = simple_strtoul(buf, &after, 10);
  317. size_t count = after - buf;
  318. DEBUG_MAX77888("[LED] %s\n", __func__);
  319. if (isspace(*after))
  320. count++;
  321. if (count == size) {
  322. ret = count;
  323. if (state > led_cdev->max_brightness)
  324. state = led_cdev->max_brightness;
  325. led_cdev->brightness = state;
  326. DEBUG_MAX77888("[LED] %s : led_cdev->brightness %d\n",
  327. __func__, led_cdev->brightness);
  328. if (!(led_cdev->flags & LED_SUSPENDED))
  329. led_cdev->brightness_set(led_cdev, state);
  330. }
  331. return ret;
  332. }
  333. static DEVICE_ATTR(rear_flash, S_IWUSR|S_IWGRP|S_IROTH,
  334. NULL, max77888_flash);
  335. static int max77888_led_probe(struct platform_device *pdev)
  336. {
  337. int ret = 0;
  338. int i;
  339. struct max77888_dev *max77888 = dev_get_drvdata(pdev->dev.parent);
  340. struct max77888_platform_data *max77888_pdata
  341. = dev_get_platdata(max77888->dev);
  342. struct max77888_led_platform_data *pdata = max77888_pdata->led_data;
  343. struct max77888_led_data *led_data;
  344. struct max77888_led *data;
  345. struct max77888_led_data **led_datas;
  346. pr_err("[zealottt] max77888_led_probe\n");
  347. if (pdata == NULL) {
  348. pr_err("[LED] no platform data for this led is found\n");
  349. return -EFAULT;
  350. }
  351. led_datas = kzalloc(sizeof(struct max77888_led_data *)
  352. * MAX77888_LED_MAX, GFP_KERNEL);
  353. if (unlikely(!led_datas)) {
  354. pr_err("[LED] memory allocation error %s", __func__);
  355. return -ENOMEM;
  356. }
  357. platform_set_drvdata(pdev, led_datas);
  358. if (led_torch_en && led_flash_en) {
  359. DEBUG_MAX77888("[LED] %s, led_torch %d, led_flash %d\n", __func__,
  360. led_torch_en, led_flash_en);
  361. flash_torch_en = 1;
  362. } else {
  363. pr_err("%s : can't find gpio", __func__);
  364. flash_torch_en = 0;
  365. }
  366. for (i = 0; i != pdata->num_leds; ++i) {
  367. data = &(pdata->leds[i]);
  368. led_data = kzalloc(sizeof(struct max77888_led_data),
  369. GFP_KERNEL);
  370. led_datas[i] = led_data;
  371. if (unlikely(!led_data)) {
  372. pr_err("[LED] memory allocation error %s\n", __func__);
  373. ret = -ENOMEM;
  374. continue;
  375. }
  376. led_data->max77888 = max77888;
  377. led_data->i2c = max77888->i2c;
  378. led_data->data = data;
  379. led_data->led.name = data->name;
  380. led_data->led.brightness_set = max77888_led_set;
  381. led_data->led.brightness = LED_OFF;
  382. led_data->brightness = data->brightness;
  383. led_data->led.flags = 0;
  384. led_data->led.max_brightness = reg_led_timer[data->id] == MAX77888_LED_REG_FLASH_TIMER
  385. ? MAX_FLASH_DRV_LEVEL : MAX_TORCH_DRV_LEVEL;
  386. mutex_init(&led_data->lock);
  387. spin_lock_init(&led_data->value_lock);
  388. INIT_WORK(&led_data->work, max77888_led_work);
  389. ret = led_classdev_register(&pdev->dev, &led_data->led);
  390. if (unlikely(ret)) {
  391. pr_err("unable to register LED\n");
  392. kfree(led_data);
  393. ret = -EFAULT;
  394. continue;
  395. }
  396. ret = max77888_led_setup(led_data);
  397. if (unlikely(ret)) {
  398. pr_err("unable to register LED\n");
  399. mutex_destroy(&led_data->lock);
  400. led_classdev_unregister(&led_data->led);
  401. kfree(led_data);
  402. ret = -EFAULT;
  403. }
  404. }
  405. /* print_all_reg_value(max77888->i2c); */
  406. if (!IS_ERR(camera_class)) {
  407. flash_dev = device_create(camera_class, NULL, 0, led_datas[1], "flash");
  408. if (flash_dev < 0)
  409. pr_err("Failed to create device(flash)!\n");
  410. if (device_create_file(flash_dev, &dev_attr_rear_flash) < 0) {
  411. pr_err("failed to create device file, %s\n",
  412. dev_attr_rear_flash.attr.name);
  413. }
  414. #ifdef CONFIG_LEDS_SEPERATE_MOVIE_FLASH
  415. if (device_create_file(flash_dev, &dev_attr_movie_brightness) < 0) {
  416. pr_err("failed to create device file, %s\n",
  417. dev_attr_movie_brightness.attr.name);
  418. }
  419. #endif
  420. } else
  421. pr_err("Failed to create device(flash) because of nothing camera class!\n");
  422. #ifdef CONFIG_LEDS_SWITCH
  423. if (system_rev < FLASH_SWITCH_REMOVED_REVISION) {
  424. if (gpio_request(GPIO_CAM_SW_EN, "CAM_SW_EN"))
  425. pr_err("failed to request CAM_SW_EN\n");
  426. else
  427. gpio_direction_output(GPIO_CAM_SW_EN, 1);
  428. }
  429. #endif
  430. return ret;
  431. }
  432. static int __devexit max77888_led_remove(struct platform_device *pdev)
  433. {
  434. struct max77888_led_data **led_datas = platform_get_drvdata(pdev);
  435. int i;
  436. for (i = 0; i != MAX77888_LED_MAX; ++i) {
  437. if (led_datas[i] == NULL)
  438. continue;
  439. cancel_work_sync(&led_datas[i]->work);
  440. mutex_destroy(&led_datas[i]->lock);
  441. led_classdev_unregister(&led_datas[i]->led);
  442. kfree(led_datas[i]);
  443. }
  444. kfree(led_datas);
  445. device_remove_file(flash_dev, &dev_attr_rear_flash);
  446. #ifdef CONFIG_LEDS_SEPERATE_MOVIE_FLASH
  447. device_remove_file(flash_dev, &dev_attr_movie_brightness);
  448. #endif
  449. device_destroy(camera_class, 0);
  450. class_destroy(camera_class);
  451. return 0;
  452. }
  453. void max77888_led_shutdown(struct device *dev)
  454. {
  455. struct max77888_led_data **led_datas = dev_get_drvdata(dev);
  456. /* Turn off LED */
  457. max77888_set_bits(led_datas[1]->i2c,
  458. MAX77888_LED_REG_FLASH_EN,
  459. led_en_mask[1],
  460. 0x02 << led_en_shift[1]);
  461. }
  462. static struct platform_driver max77888_led_driver = {
  463. .probe = max77888_led_probe,
  464. .remove = __devexit_p(max77888_led_remove),
  465. .driver = {
  466. .name = "max77888-led",
  467. .owner = THIS_MODULE,
  468. .shutdown = max77888_led_shutdown,
  469. },
  470. };
  471. static int __init max77888_led_init(void)
  472. {
  473. return platform_driver_register(&max77888_led_driver);
  474. }
  475. module_init(max77888_led_init);
  476. static void __exit max77888_led_exit(void)
  477. {
  478. platform_driver_unregister(&max77888_led_driver);
  479. }
  480. module_exit(max77888_led_exit);
  481. MODULE_AUTHOR("Samsung co. Ltd.");
  482. MODULE_DESCRIPTION("MAX77888 LED driver");
  483. MODULE_LICENSE("GPL");