leds-max8997.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * leds-max8997.c - LED class driver for MAX8997 LEDs.
  3. *
  4. * Copyright (C) 2011 Samsung Electronics
  5. * Donggeun Kim <dg77.kim@samsung.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. */
  12. #include <linux/module.h>
  13. #include <linux/err.h>
  14. #include <linux/slab.h>
  15. #include <linux/workqueue.h>
  16. #include <linux/leds.h>
  17. #include <linux/mfd/max8997.h>
  18. #include <linux/mfd/max8997-private.h>
  19. #include <linux/platform_device.h>
  20. #define MAX8997_LED_FLASH_SHIFT 3
  21. #define MAX8997_LED_FLASH_CUR_MASK 0xf8
  22. #define MAX8997_LED_MOVIE_SHIFT 4
  23. #define MAX8997_LED_MOVIE_CUR_MASK 0xf0
  24. #define MAX8997_LED_FLASH_MAX_BRIGHTNESS 0x1f
  25. #define MAX8997_LED_MOVIE_MAX_BRIGHTNESS 0xf
  26. #define MAX8997_LED_NONE_MAX_BRIGHTNESS 0
  27. #define MAX8997_LED0_FLASH_MASK 0x1
  28. #define MAX8997_LED0_FLASH_PIN_MASK 0x5
  29. #define MAX8997_LED0_MOVIE_MASK 0x8
  30. #define MAX8997_LED0_MOVIE_PIN_MASK 0x28
  31. #define MAX8997_LED1_FLASH_MASK 0x2
  32. #define MAX8997_LED1_FLASH_PIN_MASK 0x6
  33. #define MAX8997_LED1_MOVIE_MASK 0x10
  34. #define MAX8997_LED1_MOVIE_PIN_MASK 0x30
  35. #define MAX8997_LED_BOOST_ENABLE_MASK (1 << 6)
  36. struct max8997_led {
  37. struct max8997_dev *iodev;
  38. struct led_classdev cdev;
  39. bool enabled;
  40. int id;
  41. enum max8997_led_mode led_mode;
  42. struct mutex mutex;
  43. };
  44. static void max8997_led_set_mode(struct max8997_led *led,
  45. enum max8997_led_mode mode)
  46. {
  47. int ret;
  48. struct i2c_client *client = led->iodev->i2c;
  49. u8 mask = 0, val;
  50. switch (mode) {
  51. case MAX8997_FLASH_MODE:
  52. mask = MAX8997_LED1_FLASH_MASK | MAX8997_LED0_FLASH_MASK;
  53. val = led->id ?
  54. MAX8997_LED1_FLASH_MASK : MAX8997_LED0_FLASH_MASK;
  55. led->cdev.max_brightness = MAX8997_LED_FLASH_MAX_BRIGHTNESS;
  56. break;
  57. case MAX8997_MOVIE_MODE:
  58. mask = MAX8997_LED1_MOVIE_MASK | MAX8997_LED0_MOVIE_MASK;
  59. val = led->id ?
  60. MAX8997_LED1_MOVIE_MASK : MAX8997_LED0_MOVIE_MASK;
  61. led->cdev.max_brightness = MAX8997_LED_MOVIE_MAX_BRIGHTNESS;
  62. break;
  63. case MAX8997_FLASH_PIN_CONTROL_MODE:
  64. mask = MAX8997_LED1_FLASH_PIN_MASK |
  65. MAX8997_LED0_FLASH_PIN_MASK;
  66. val = led->id ?
  67. MAX8997_LED1_FLASH_PIN_MASK : MAX8997_LED0_FLASH_PIN_MASK;
  68. led->cdev.max_brightness = MAX8997_LED_FLASH_MAX_BRIGHTNESS;
  69. break;
  70. case MAX8997_MOVIE_PIN_CONTROL_MODE:
  71. mask = MAX8997_LED1_MOVIE_PIN_MASK |
  72. MAX8997_LED0_MOVIE_PIN_MASK;
  73. val = led->id ?
  74. MAX8997_LED1_MOVIE_PIN_MASK : MAX8997_LED0_MOVIE_PIN_MASK;
  75. led->cdev.max_brightness = MAX8997_LED_MOVIE_MAX_BRIGHTNESS;
  76. break;
  77. default:
  78. led->cdev.max_brightness = MAX8997_LED_NONE_MAX_BRIGHTNESS;
  79. break;
  80. }
  81. if (mask) {
  82. ret = max8997_update_reg(client, MAX8997_REG_LEN_CNTL, val,
  83. mask);
  84. if (ret)
  85. dev_err(led->iodev->dev,
  86. "failed to update register(%d)\n", ret);
  87. }
  88. led->led_mode = mode;
  89. }
  90. static void max8997_led_enable(struct max8997_led *led, bool enable)
  91. {
  92. int ret;
  93. struct i2c_client *client = led->iodev->i2c;
  94. u8 val = 0, mask = MAX8997_LED_BOOST_ENABLE_MASK;
  95. if (led->enabled == enable)
  96. return;
  97. val = enable ? MAX8997_LED_BOOST_ENABLE_MASK : 0;
  98. ret = max8997_update_reg(client, MAX8997_REG_BOOST_CNTL, val, mask);
  99. if (ret)
  100. dev_err(led->iodev->dev,
  101. "failed to update register(%d)\n", ret);
  102. led->enabled = enable;
  103. }
  104. static void max8997_led_set_current(struct max8997_led *led,
  105. enum led_brightness value)
  106. {
  107. int ret;
  108. struct i2c_client *client = led->iodev->i2c;
  109. u8 val = 0, mask = 0, reg = 0;
  110. switch (led->led_mode) {
  111. case MAX8997_FLASH_MODE:
  112. case MAX8997_FLASH_PIN_CONTROL_MODE:
  113. val = value << MAX8997_LED_FLASH_SHIFT;
  114. mask = MAX8997_LED_FLASH_CUR_MASK;
  115. reg = led->id ? MAX8997_REG_FLASH2_CUR : MAX8997_REG_FLASH1_CUR;
  116. break;
  117. case MAX8997_MOVIE_MODE:
  118. case MAX8997_MOVIE_PIN_CONTROL_MODE:
  119. val = value << MAX8997_LED_MOVIE_SHIFT;
  120. mask = MAX8997_LED_MOVIE_CUR_MASK;
  121. reg = MAX8997_REG_MOVIE_CUR;
  122. break;
  123. default:
  124. break;
  125. }
  126. if (mask) {
  127. ret = max8997_update_reg(client, reg, val, mask);
  128. if (ret)
  129. dev_err(led->iodev->dev,
  130. "failed to update register(%d)\n", ret);
  131. }
  132. }
  133. static void max8997_led_brightness_set(struct led_classdev *led_cdev,
  134. enum led_brightness value)
  135. {
  136. struct max8997_led *led =
  137. container_of(led_cdev, struct max8997_led, cdev);
  138. if (value) {
  139. max8997_led_set_current(led, value);
  140. max8997_led_enable(led, true);
  141. } else {
  142. max8997_led_set_current(led, value);
  143. max8997_led_enable(led, false);
  144. }
  145. }
  146. static ssize_t max8997_led_show_mode(struct device *dev,
  147. struct device_attribute *attr, char *buf)
  148. {
  149. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  150. struct max8997_led *led =
  151. container_of(led_cdev, struct max8997_led, cdev);
  152. ssize_t ret = 0;
  153. mutex_lock(&led->mutex);
  154. switch (led->led_mode) {
  155. case MAX8997_FLASH_MODE:
  156. ret += sprintf(buf, "FLASH\n");
  157. break;
  158. case MAX8997_MOVIE_MODE:
  159. ret += sprintf(buf, "MOVIE\n");
  160. break;
  161. case MAX8997_FLASH_PIN_CONTROL_MODE:
  162. ret += sprintf(buf, "FLASH_PIN_CONTROL\n");
  163. break;
  164. case MAX8997_MOVIE_PIN_CONTROL_MODE:
  165. ret += sprintf(buf, "MOVIE_PIN_CONTROL\n");
  166. break;
  167. default:
  168. ret += sprintf(buf, "NONE\n");
  169. break;
  170. }
  171. mutex_unlock(&led->mutex);
  172. return ret;
  173. }
  174. static ssize_t max8997_led_store_mode(struct device *dev,
  175. struct device_attribute *attr,
  176. const char *buf, size_t size)
  177. {
  178. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  179. struct max8997_led *led =
  180. container_of(led_cdev, struct max8997_led, cdev);
  181. enum max8997_led_mode mode;
  182. mutex_lock(&led->mutex);
  183. if (!strncmp(buf, "FLASH_PIN_CONTROL", 17))
  184. mode = MAX8997_FLASH_PIN_CONTROL_MODE;
  185. else if (!strncmp(buf, "MOVIE_PIN_CONTROL", 17))
  186. mode = MAX8997_MOVIE_PIN_CONTROL_MODE;
  187. else if (!strncmp(buf, "FLASH", 5))
  188. mode = MAX8997_FLASH_MODE;
  189. else if (!strncmp(buf, "MOVIE", 5))
  190. mode = MAX8997_MOVIE_MODE;
  191. else
  192. mode = MAX8997_NONE;
  193. max8997_led_set_mode(led, mode);
  194. mutex_unlock(&led->mutex);
  195. return size;
  196. }
  197. static DEVICE_ATTR(mode, 0644, max8997_led_show_mode, max8997_led_store_mode);
  198. static struct attribute *max8997_attrs[] = {
  199. &dev_attr_mode.attr,
  200. NULL
  201. };
  202. ATTRIBUTE_GROUPS(max8997);
  203. static int max8997_led_probe(struct platform_device *pdev)
  204. {
  205. struct max8997_dev *iodev = dev_get_drvdata(pdev->dev.parent);
  206. struct max8997_platform_data *pdata = dev_get_platdata(iodev->dev);
  207. struct max8997_led *led;
  208. char name[20];
  209. int ret = 0;
  210. if (pdata == NULL) {
  211. dev_err(&pdev->dev, "no platform data\n");
  212. return -ENODEV;
  213. }
  214. led = devm_kzalloc(&pdev->dev, sizeof(*led), GFP_KERNEL);
  215. if (led == NULL)
  216. return -ENOMEM;
  217. led->id = pdev->id;
  218. snprintf(name, sizeof(name), "max8997-led%d", pdev->id);
  219. led->cdev.name = name;
  220. led->cdev.brightness_set = max8997_led_brightness_set;
  221. led->cdev.flags |= LED_CORE_SUSPENDRESUME;
  222. led->cdev.brightness = 0;
  223. led->cdev.groups = max8997_groups;
  224. led->iodev = iodev;
  225. /* initialize mode and brightness according to platform_data */
  226. if (pdata->led_pdata) {
  227. u8 mode = 0, brightness = 0;
  228. mode = pdata->led_pdata->mode[led->id];
  229. brightness = pdata->led_pdata->brightness[led->id];
  230. max8997_led_set_mode(led, pdata->led_pdata->mode[led->id]);
  231. if (brightness > led->cdev.max_brightness)
  232. brightness = led->cdev.max_brightness;
  233. max8997_led_set_current(led, brightness);
  234. led->cdev.brightness = brightness;
  235. } else {
  236. max8997_led_set_mode(led, MAX8997_NONE);
  237. max8997_led_set_current(led, 0);
  238. }
  239. mutex_init(&led->mutex);
  240. platform_set_drvdata(pdev, led);
  241. ret = led_classdev_register(&pdev->dev, &led->cdev);
  242. if (ret < 0)
  243. return ret;
  244. return 0;
  245. }
  246. static int max8997_led_remove(struct platform_device *pdev)
  247. {
  248. struct max8997_led *led = platform_get_drvdata(pdev);
  249. led_classdev_unregister(&led->cdev);
  250. return 0;
  251. }
  252. static struct platform_driver max8997_led_driver = {
  253. .driver = {
  254. .name = "max8997-led",
  255. },
  256. .probe = max8997_led_probe,
  257. .remove = max8997_led_remove,
  258. };
  259. module_platform_driver(max8997_led_driver);
  260. MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
  261. MODULE_DESCRIPTION("MAX8997 LED driver");
  262. MODULE_LICENSE("GPL");
  263. MODULE_ALIAS("platform:max8997-led");