Advertisement
Guest User

Untitled

a guest
May 1st, 2012
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.46 KB | None | 0 0
  1. /* linux/arch/arm/mach-msm/board-htcwhitestone-led.c
  2. *
  3. * Copyright (2011) Oliver Gjoneski <ogjoneski@gmail.com>
  4. *
  5. * Based on board-htctopaz-led.c:
  6. * Copyright (2011) Michael Weirauch <mweirauch@xdandroid.com>
  7. *
  8. * Based on leds-microp-htckovsky.c:
  9. * Copyright (2010) Alexander Tarasikov <alexander.tarasikov@gmail.com>
  10. * Some code was written by ultrashot at xda-developers
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. */
  26. #include <linux/module.h>
  27. #include <linux/debugfs.h>
  28. #include <linux/init.h>
  29. #include <linux/i2c.h>
  30. #include <linux/leds.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/workqueue.h>
  33. #include <linux/microp.h>
  34. #include <linux/microp-ksc.h>
  35. #include <linux/microp-ng.h>
  36. #include <asm/mach-types.h>
  37.  
  38. static uint8_t g_auto_backlight = 0;
  39.  
  40. struct workqueue_struct *whitestone_led_wq;
  41. EXPORT_SYMBOL(whitestone_led_wq);
  42. static struct work_struct lcd_work, auto_bl_work;
  43.  
  44. static void htcwhitestone_led_brightness_update(struct work_struct* work);
  45.  
  46. static void htcwhitestone_led_brightness_set(struct led_classdev *led_cdev, enum led_brightness brightness);
  47.  
  48. static struct i2c_client *client = NULL;
  49. static struct i2c_client *ksc_client=NULL;
  50.  
  51. enum supported_led {LCD};
  52. static struct led_classdev htcwhitestone_leds[] = {
  53. [LCD] = {
  54. .name = "lcd-backlight",
  55. .brightness = 0x90,
  56. .brightness_set = htcwhitestone_led_brightness_set,
  57. },
  58. };
  59. /*Worker functions*/
  60.  
  61. static void htcwhitestone_led_brightness_update(struct work_struct* work)
  62. {
  63. uint8_t buffer[2] = {0, 0};
  64. enum led_brightness brightness = htcwhitestone_leds[LCD].brightness;
  65.  
  66. if (!client) {
  67. return;
  68. }
  69.  
  70. buffer[0] = 0x22; // value from .27 microp-klt.c
  71. buffer[1] = (brightness*9)/255;
  72.  
  73. printk(KERN_DEBUG "%s:Setting LCD brightness to %d/10\n", __func__, buffer[1]);
  74.  
  75. microp_ng_write(client, buffer, 2);
  76.  
  77. return;
  78. }
  79.  
  80. static void htcwhitestone_auto_backlight_update(struct work_struct *work)
  81. {
  82. int ret;
  83. uint8_t buf[3] = { 0, 0, 0 };
  84.  
  85. if (!client) {
  86. return;
  87. }
  88.  
  89. printk(KERN_DEBUG "%s: %s (%d)\n", __func__, g_auto_backlight ? "on" : "off", g_auto_backlight);
  90.  
  91. buf[0] = 0x23;
  92. buf[1] = g_auto_backlight ? 1 : 0;
  93. buf[2] = g_auto_backlight ? 1 : 0;
  94.  
  95. ret = microp_ng_write(client, buf, ARRAY_SIZE(buf));
  96. if (ret) {
  97. printk(KERN_ERR "%s: Failed writing auto backlight status (%d)\n",
  98. __func__, ret);
  99. }
  100. return;
  101. }
  102. /*brightness_set callback functions for all htcwhitestone_leds[]*/
  103. static void htcwhitestone_led_brightness_set(struct led_classdev *led_cdev, enum led_brightness brightness)
  104. {
  105.  
  106. if ( !strcmp(led_cdev->name, "klt::lcd-bkl") || !strcmp(led_cdev->name, "lcd-backlight"))
  107. queue_work(whitestone_led_wq, &lcd_work);
  108. return;
  109.  
  110. }
  111.  
  112. /*
  113. * dev_attr_auto_backlight for lcd-backlight led device
  114. */
  115.  
  116. static ssize_t htcwhitestone_auto_backlight_get(struct device *dev,
  117. struct device_attribute *attr, char *ret_buf)
  118. {
  119. return sprintf(ret_buf,"%d\n",g_auto_backlight);
  120. }
  121.  
  122. static ssize_t htcwhitestone_auto_backlight_set(struct device *dev,
  123. struct device_attribute *attr, const char *in_buf, size_t count)
  124. {
  125. unsigned long val = simple_strtoul(in_buf, NULL, 10);
  126. g_auto_backlight = val ? 1 : 0;
  127.  
  128. queue_work(whitestone_led_wq, &auto_bl_work);
  129.  
  130. return count;
  131. }
  132.  
  133. static DEVICE_ATTR(auto_backlight, 0644, htcwhitestone_auto_backlight_get,
  134. htcwhitestone_auto_backlight_set);
  135.  
  136. static int htcwhitestone_microp_probe(struct platform_device *pdev)
  137. {
  138. int ret = 0;
  139. int i;
  140.  
  141. printk(KERN_INFO "%s\n", __func__);
  142. client = dev_get_drvdata(&pdev->dev);
  143. ksc_client=i2c_new_dummy(client->adapter, 0x67);
  144. // if((!ksc_client) || (!client)) {
  145. // printk("%s failed to obtain client or ksc_client\n",__func__);
  146. // }
  147.  
  148. for (i = 0; i < ARRAY_SIZE(htcwhitestone_leds); i++) {
  149. ret = led_classdev_register(&pdev->dev, &htcwhitestone_leds[i]);
  150. if (ret < 0) {
  151. printk(KERN_ERR "%s: Failed registering led %s", __func__,
  152. htcwhitestone_leds[i].name);
  153. goto led_fail;
  154. }
  155. }
  156.  
  157. ret = device_create_file(htcwhitestone_leds[LCD].dev, &dev_attr_auto_backlight);
  158. if (ret < 0) {
  159. printk(KERN_ERR "%s: Failed registering auto_backlight attribute (%d)",
  160. __func__, ret);
  161. goto led_fail;
  162. }
  163.  
  164. INIT_WORK(&lcd_work, htcwhitestone_led_brightness_update);
  165. INIT_WORK(&auto_bl_work, htcwhitestone_auto_backlight_update);
  166.  
  167. // some defaults at boot
  168. queue_work(whitestone_led_wq, &lcd_work);
  169.  
  170. return 0;
  171.  
  172. led_fail:
  173. for (i--; i >= 0; i--) {
  174. led_classdev_unregister(&htcwhitestone_leds[i]);
  175. }
  176. return ret;
  177. }
  178.  
  179. static int htcwhitestone_microp_remove(struct platform_device *pdev)
  180. {
  181. int i;
  182.  
  183. for (i = 0; i < ARRAY_SIZE(htcwhitestone_leds); i++) {
  184. led_classdev_unregister(&htcwhitestone_leds[i]);
  185. }
  186. client = NULL;
  187. destroy_workqueue(whitestone_led_wq);
  188. return 0;
  189. }
  190.  
  191. #if CONFIG_PM
  192. static int htcwhitestone_microp_suspend(struct platform_device *pdev, pm_message_t mesg)
  193. {
  194. flush_workqueue(whitestone_led_wq);
  195.  
  196. return 0;
  197. }
  198.  
  199. static int htcwhitestone_microp_resume(struct platform_device *pdev)
  200. {
  201.  
  202. return 0;
  203. }
  204. #else
  205. #define htcwhitestone_microp_suspend NULL
  206. #define htcwhitestone_microp_resume NULL
  207. #endif
  208.  
  209. static struct platform_driver htcwhitestone_microp_driver = {
  210. .probe = htcwhitestone_microp_probe,
  211. .remove = htcwhitestone_microp_remove,
  212. .suspend = htcwhitestone_microp_suspend,
  213. .resume = htcwhitestone_microp_resume,
  214. .driver = {
  215. .name = "htcwhitestone-microp-leds",
  216. .owner = THIS_MODULE,
  217. },
  218. };
  219.  
  220. static int __init htcwhitestone_microp_init(void)
  221. {
  222. whitestone_led_wq = create_singlethread_workqueue("led_wq");
  223. if (whitestone_led_wq == 0)
  224. return -ENOMEM;
  225. return platform_driver_register(&htcwhitestone_microp_driver);
  226. }
  227.  
  228. static void __exit htcwhitestone_microp_exit(void)
  229. {
  230. platform_driver_unregister(&htcwhitestone_microp_driver);
  231. }
  232.  
  233. module_init(htcwhitestone_microp_init);
  234. module_exit(htcwhitestone_microp_exit)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement