leds-max8997.c 7.7 KB

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