leds-msm-tricolor.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /* Copyright (c) 2012, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/slab.h>
  15. #include <linux/leds.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/err.h>
  18. #include <linux/leds-msm-tricolor.h>
  19. #include <mach/msm_rpcrouter.h>
  20. #define LED_RPC_PROG 0x30000091
  21. #define LED_RPC_VER 0x00030001
  22. #define LED_SUBSCRIBE_PROC 0x03
  23. #define LED_SUBS_RCV_EVNT 0x01
  24. #define LED_SUBS_REGISTER 0x00
  25. #define LED_EVNT_CLASS_ALL 0x00
  26. #define LINUX_HOST 0x04
  27. #define LED_CMD_PROC 0x02
  28. #define TRICOLOR_LED_ID 0x0A
  29. enum tricolor_led_status {
  30. ALL_OFF,
  31. ALL_ON,
  32. BLUE_ON,
  33. BLUE_OFF,
  34. RED_ON,
  35. RED_OFF,
  36. GREEN_ON,
  37. GREEN_OFF,
  38. BLUE_BLINK,
  39. RED_BLINK,
  40. GREEN_BLINK,
  41. BLUE_BLINK_OFF,
  42. RED_BLINK_OFF,
  43. GREEN_BLINK_OFF,
  44. LED_MAX,
  45. };
  46. struct led_cmd_data_type {
  47. u32 cmd_data_type_ptr; /* cmd_data_type ptr */
  48. u32 ver; /* version */
  49. u32 id; /* command id */
  50. u32 handle; /* handle returned from subscribe proc */
  51. u32 disc_id1; /* discriminator id */
  52. u32 input_ptr; /* input ptr length */
  53. u32 input_val; /* command specific data */
  54. u32 input_len; /* length of command input */
  55. u32 disc_id2; /* discriminator id */
  56. u32 output_len; /* length of output data */
  57. u32 delayed; /* execution context for modem */
  58. };
  59. struct led_subscribe_req {
  60. u32 subs_ptr; /* subscribe ptr */
  61. u32 ver; /* version */
  62. u32 srvc; /* command or event */
  63. u32 req; /* subscribe or unsubscribe */
  64. u32 host_os; /* host operating system */
  65. u32 disc_id; /* discriminator id */
  66. u32 event; /* event */
  67. u32 cb_id; /* callback id */
  68. u32 handle_ptr; /* handle ptr */
  69. u32 handle_data; /* handle data */
  70. };
  71. struct tricolor_led_data {
  72. struct led_classdev cdev;
  73. struct msm_rpc_client *rpc_client;
  74. bool blink_status;
  75. struct mutex lock;
  76. u8 color;
  77. };
  78. static struct led_subscribe_req *led_subs_req;
  79. static int led_send_cmd_arg(struct msm_rpc_client *client,
  80. void *buffer, void *data)
  81. {
  82. struct led_cmd_data_type *led_cmd = buffer;
  83. enum tricolor_led_status status = *(enum tricolor_led_status *) data;
  84. led_cmd->cmd_data_type_ptr = cpu_to_be32(0x01);
  85. led_cmd->ver = cpu_to_be32(0x03);
  86. led_cmd->id = cpu_to_be32(TRICOLOR_LED_ID);
  87. led_cmd->handle = cpu_to_be32(led_subs_req->handle_data);
  88. led_cmd->disc_id1 = cpu_to_be32(TRICOLOR_LED_ID);
  89. led_cmd->input_ptr = cpu_to_be32(0x01);
  90. led_cmd->input_val = cpu_to_be32(status);
  91. led_cmd->input_len = cpu_to_be32(0x01);
  92. led_cmd->disc_id2 = cpu_to_be32(TRICOLOR_LED_ID);
  93. led_cmd->output_len = cpu_to_be32(0x00);
  94. led_cmd->delayed = cpu_to_be32(0x00);
  95. return sizeof(*led_cmd);
  96. }
  97. static int led_rpc_res(struct msm_rpc_client *client,
  98. void *buffer, void *data)
  99. {
  100. uint32_t result;
  101. result = be32_to_cpu(*((uint32_t *)buffer));
  102. pr_debug("%s: request completed: 0x%x\n", __func__, result);
  103. return 0;
  104. }
  105. static void led_rpc_set_status(struct msm_rpc_client *client,
  106. enum tricolor_led_status status)
  107. {
  108. int rc;
  109. rc = msm_rpc_client_req(client, LED_CMD_PROC,
  110. led_send_cmd_arg, &status, led_rpc_res, NULL, -1);
  111. if (rc)
  112. pr_err("%s: RPC client request for led failed", __func__);
  113. }
  114. static ssize_t led_blink_show(struct device *dev,
  115. struct device_attribute *attr,
  116. char *buf)
  117. {
  118. struct tricolor_led_data *led = dev_get_drvdata(dev);
  119. return snprintf(buf, 2, "%d\n", led->blink_status);
  120. }
  121. static ssize_t led_blink_store(struct device *dev,
  122. struct device_attribute *attr,
  123. const char *buf, size_t size)
  124. {
  125. struct tricolor_led_data *led = dev_get_drvdata(dev);
  126. enum tricolor_led_status status;
  127. unsigned long value;
  128. int rc;
  129. if (size > 2)
  130. return -EINVAL;
  131. rc = kstrtoul(buf, 10, &value);
  132. if (rc)
  133. return rc;
  134. if (value < LED_OFF || value > led->cdev.max_brightness) {
  135. dev_err(dev, "invalid brightness\n");
  136. return -EINVAL;
  137. }
  138. switch (led->color) {
  139. case LED_COLOR_RED:
  140. status = value ? RED_BLINK : RED_BLINK_OFF;
  141. break;
  142. case LED_COLOR_GREEN:
  143. status = value ? GREEN_BLINK : GREEN_BLINK_OFF;
  144. break;
  145. case LED_COLOR_BLUE:
  146. status = value ? BLUE_BLINK : BLUE_BLINK_OFF;
  147. break;
  148. default:
  149. dev_err(dev, "unknown led device\n");
  150. return -EINVAL;
  151. }
  152. mutex_lock(&led->lock);
  153. led->blink_status = !!value;
  154. led->cdev.brightness = 0;
  155. /* program the led blink */
  156. led_rpc_set_status(led->rpc_client, status);
  157. mutex_unlock(&led->lock);
  158. return size;
  159. }
  160. static DEVICE_ATTR(blink, 0644, led_blink_show, led_blink_store);
  161. static void tricolor_led_set(struct led_classdev *led_cdev,
  162. enum led_brightness value)
  163. {
  164. struct tricolor_led_data *led;
  165. enum tricolor_led_status status;
  166. led = container_of(led_cdev, struct tricolor_led_data, cdev);
  167. if (value < LED_OFF || value > led->cdev.max_brightness) {
  168. dev_err(led->cdev.dev, "invalid brightness\n");
  169. return;
  170. }
  171. switch (led->color) {
  172. case LED_COLOR_RED:
  173. status = value ? RED_ON : RED_OFF;
  174. break;
  175. case LED_COLOR_GREEN:
  176. status = value ? GREEN_ON : GREEN_OFF;
  177. break;
  178. case LED_COLOR_BLUE:
  179. status = value ? BLUE_ON : BLUE_OFF;
  180. break;
  181. default:
  182. dev_err(led->cdev.dev, "unknown led device\n");
  183. return;
  184. }
  185. mutex_lock(&led->lock);
  186. led->blink_status = 0;
  187. led->cdev.brightness = value;
  188. /* program the led brightness */
  189. led_rpc_set_status(led->rpc_client, status);
  190. mutex_unlock(&led->lock);
  191. }
  192. static enum led_brightness tricolor_led_get(struct led_classdev *led_cdev)
  193. {
  194. struct tricolor_led_data *led;
  195. led = container_of(led_cdev, struct tricolor_led_data, cdev);
  196. return led->cdev.brightness;
  197. }
  198. static int led_rpc_register_subs_arg(struct msm_rpc_client *client,
  199. void *buffer, void *data)
  200. {
  201. led_subs_req = buffer;
  202. led_subs_req->subs_ptr = cpu_to_be32(0x1);
  203. led_subs_req->ver = cpu_to_be32(0x1);
  204. led_subs_req->srvc = cpu_to_be32(LED_SUBS_RCV_EVNT);
  205. led_subs_req->req = cpu_to_be32(LED_SUBS_REGISTER);
  206. led_subs_req->host_os = cpu_to_be32(LINUX_HOST);
  207. led_subs_req->disc_id = cpu_to_be32(LED_SUBS_RCV_EVNT);
  208. led_subs_req->event = cpu_to_be32(LED_EVNT_CLASS_ALL);
  209. led_subs_req->cb_id = cpu_to_be32(0x1);
  210. led_subs_req->handle_ptr = cpu_to_be32(0x1);
  211. led_subs_req->handle_data = cpu_to_be32(0x0);
  212. return sizeof(*led_subs_req);
  213. }
  214. static int led_cb_func(struct msm_rpc_client *client, void *buffer, int in_size)
  215. {
  216. struct rpc_request_hdr *hdr = buffer;
  217. int rc;
  218. hdr->type = be32_to_cpu(hdr->type);
  219. hdr->xid = be32_to_cpu(hdr->xid);
  220. hdr->rpc_vers = be32_to_cpu(hdr->rpc_vers);
  221. hdr->prog = be32_to_cpu(hdr->prog);
  222. hdr->vers = be32_to_cpu(hdr->vers);
  223. hdr->procedure = be32_to_cpu(hdr->procedure);
  224. msm_rpc_start_accepted_reply(client, hdr->xid,
  225. RPC_ACCEPTSTAT_SUCCESS);
  226. rc = msm_rpc_send_accepted_reply(client, 0);
  227. if (rc)
  228. pr_err("%s: sending reply failed: %d\n", __func__, rc);
  229. return rc;
  230. }
  231. static int __devinit tricolor_led_probe(struct platform_device *pdev)
  232. {
  233. const struct led_platform_data *pdata = pdev->dev.platform_data;
  234. struct msm_rpc_client *rpc_client;
  235. struct led_info *curr_led;
  236. struct tricolor_led_data *led, *tmp_led;
  237. int rc, i, j;
  238. if (!pdata) {
  239. dev_err(&pdev->dev, "platform data not supplied\n");
  240. return -EINVAL;
  241. }
  242. /* initialize rpc client */
  243. rpc_client = msm_rpc_register_client("led", LED_RPC_PROG,
  244. LED_RPC_VER, 0, led_cb_func);
  245. rc = IS_ERR(rpc_client);
  246. if (rc) {
  247. dev_err(&pdev->dev, "failed to initialize rpc_client\n");
  248. return -EINVAL;
  249. }
  250. /* subscribe */
  251. rc = msm_rpc_client_req(rpc_client, LED_SUBSCRIBE_PROC,
  252. led_rpc_register_subs_arg, NULL,
  253. led_rpc_res, NULL, -1);
  254. if (rc) {
  255. pr_err("%s: RPC client request failed for subscribe services\n",
  256. __func__);
  257. goto fail_mem_alloc;
  258. }
  259. led = devm_kzalloc(&pdev->dev, pdata->num_leds * sizeof(*led),
  260. GFP_KERNEL);
  261. if (!led) {
  262. dev_err(&pdev->dev, "failed to alloc memory\n");
  263. rc = -ENOMEM;
  264. goto fail_mem_alloc;
  265. }
  266. for (i = 0; i < pdata->num_leds; i++) {
  267. curr_led = &pdata->leds[i];
  268. tmp_led = &led[i];
  269. tmp_led->cdev.name = curr_led->name;
  270. tmp_led->cdev.default_trigger = curr_led->default_trigger;
  271. tmp_led->cdev.brightness_set = tricolor_led_set;
  272. tmp_led->cdev.brightness_get = tricolor_led_get;
  273. tmp_led->cdev.brightness = LED_OFF;
  274. tmp_led->cdev.max_brightness = LED_FULL;
  275. tmp_led->color = curr_led->flags;
  276. tmp_led->rpc_client = rpc_client;
  277. tmp_led->blink_status = false;
  278. mutex_init(&tmp_led->lock);
  279. rc = led_classdev_register(&pdev->dev, &tmp_led->cdev);
  280. if (rc) {
  281. dev_err(&pdev->dev, "failed to register led %s(%d)\n",
  282. tmp_led->cdev.name, rc);
  283. goto fail_led_reg;
  284. }
  285. /* Add blink attributes */
  286. rc = device_create_file(tmp_led->cdev.dev, &dev_attr_blink);
  287. if (rc) {
  288. dev_err(&pdev->dev, "failed to create blink attr\n");
  289. goto fail_blink_attr;
  290. }
  291. dev_set_drvdata(tmp_led->cdev.dev, tmp_led);
  292. }
  293. platform_set_drvdata(pdev, led);
  294. return 0;
  295. fail_blink_attr:
  296. j = i;
  297. while (j)
  298. device_remove_file(led[--j].cdev.dev, &dev_attr_blink);
  299. i++;
  300. fail_led_reg:
  301. while (i) {
  302. led_classdev_unregister(&led[--i].cdev);
  303. mutex_destroy(&led[i].lock);
  304. }
  305. fail_mem_alloc:
  306. msm_rpc_unregister_client(rpc_client);
  307. return rc;
  308. }
  309. static int __devexit tricolor_led_remove(struct platform_device *pdev)
  310. {
  311. const struct led_platform_data *pdata = pdev->dev.platform_data;
  312. struct tricolor_led_data *led = platform_get_drvdata(pdev);
  313. int i;
  314. for (i = 0; i < pdata->num_leds; i++) {
  315. led_classdev_unregister(&led[i].cdev);
  316. device_remove_file(led[i].cdev.dev, &dev_attr_blink);
  317. mutex_destroy(&led[i].lock);
  318. }
  319. msm_rpc_unregister_client(led->rpc_client);
  320. return 0;
  321. }
  322. static struct platform_driver tricolor_led_driver = {
  323. .probe = tricolor_led_probe,
  324. .remove = __devexit_p(tricolor_led_remove),
  325. .driver = {
  326. .name = "msm-tricolor-leds",
  327. .owner = THIS_MODULE,
  328. },
  329. };
  330. static int __init tricolor_led_init(void)
  331. {
  332. return platform_driver_register(&tricolor_led_driver);
  333. }
  334. late_initcall(tricolor_led_init);
  335. static void __exit tricolor_led_exit(void)
  336. {
  337. platform_driver_unregister(&tricolor_led_driver);
  338. }
  339. module_exit(tricolor_led_exit);
  340. MODULE_DESCRIPTION("MSM Tri-color LEDs driver");
  341. MODULE_LICENSE("GPL v2");
  342. MODULE_VERSION("1.0");
  343. MODULE_ALIAS("platform:tricolor-led");