light.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364
  1. /*
  2. * Greybus Lights protocol driver.
  3. *
  4. * Copyright 2015 Google Inc.
  5. * Copyright 2015 Linaro Ltd.
  6. *
  7. * Released under the GPLv2 only.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/leds.h>
  11. #include <linux/led-class-flash.h>
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <linux/version.h>
  15. #include <media/v4l2-flash-led-class.h>
  16. #include "greybus.h"
  17. #include "greybus_protocols.h"
  18. #define NAMES_MAX 32
  19. struct gb_channel {
  20. u8 id;
  21. u32 flags;
  22. u32 color;
  23. char *color_name;
  24. u8 fade_in;
  25. u8 fade_out;
  26. u32 mode;
  27. char *mode_name;
  28. struct attribute **attrs;
  29. struct attribute_group *attr_group;
  30. const struct attribute_group **attr_groups;
  31. struct led_classdev *led;
  32. #if IS_REACHABLE(CONFIG_LEDS_CLASS_FLASH)
  33. struct led_classdev_flash fled;
  34. struct led_flash_setting intensity_uA;
  35. struct led_flash_setting timeout_us;
  36. #else
  37. struct led_classdev cled;
  38. #endif
  39. struct gb_light *light;
  40. bool is_registered;
  41. bool releasing;
  42. bool strobe_state;
  43. bool active;
  44. struct mutex lock;
  45. };
  46. struct gb_light {
  47. u8 id;
  48. char *name;
  49. struct gb_lights *glights;
  50. u32 flags;
  51. u8 channels_count;
  52. struct gb_channel *channels;
  53. bool has_flash;
  54. bool ready;
  55. #if IS_REACHABLE(CONFIG_V4L2_FLASH_LED_CLASS)
  56. struct v4l2_flash *v4l2_flash;
  57. #endif
  58. };
  59. struct gb_lights {
  60. struct gb_connection *connection;
  61. u8 lights_count;
  62. struct gb_light *lights;
  63. struct mutex lights_lock;
  64. };
  65. static void gb_lights_channel_free(struct gb_channel *channel);
  66. static struct gb_connection *get_conn_from_channel(struct gb_channel *channel)
  67. {
  68. return channel->light->glights->connection;
  69. }
  70. static struct gb_connection *get_conn_from_light(struct gb_light *light)
  71. {
  72. return light->glights->connection;
  73. }
  74. static bool is_channel_flash(struct gb_channel *channel)
  75. {
  76. return !!(channel->mode & (GB_CHANNEL_MODE_FLASH | GB_CHANNEL_MODE_TORCH
  77. | GB_CHANNEL_MODE_INDICATOR));
  78. }
  79. #if IS_REACHABLE(CONFIG_LEDS_CLASS_FLASH)
  80. static struct gb_channel *get_channel_from_cdev(struct led_classdev *cdev)
  81. {
  82. struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(cdev);
  83. return container_of(fled_cdev, struct gb_channel, fled);
  84. }
  85. static struct led_classdev *get_channel_cdev(struct gb_channel *channel)
  86. {
  87. return &channel->fled.led_cdev;
  88. }
  89. static struct gb_channel *get_channel_from_mode(struct gb_light *light,
  90. u32 mode)
  91. {
  92. struct gb_channel *channel = NULL;
  93. int i;
  94. for (i = 0; i < light->channels_count; i++) {
  95. channel = &light->channels[i];
  96. if (channel && channel->mode == mode)
  97. break;
  98. }
  99. return channel;
  100. }
  101. static int __gb_lights_flash_intensity_set(struct gb_channel *channel,
  102. u32 intensity)
  103. {
  104. struct gb_connection *connection = get_conn_from_channel(channel);
  105. struct gb_bundle *bundle = connection->bundle;
  106. struct gb_lights_set_flash_intensity_request req;
  107. int ret;
  108. if (channel->releasing)
  109. return -ESHUTDOWN;
  110. ret = gb_pm_runtime_get_sync(bundle);
  111. if (ret < 0)
  112. return ret;
  113. req.light_id = channel->light->id;
  114. req.channel_id = channel->id;
  115. req.intensity_uA = cpu_to_le32(intensity);
  116. ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_FLASH_INTENSITY,
  117. &req, sizeof(req), NULL, 0);
  118. gb_pm_runtime_put_autosuspend(bundle);
  119. return ret;
  120. }
  121. static int __gb_lights_flash_brightness_set(struct gb_channel *channel)
  122. {
  123. u32 intensity;
  124. /* If the channel is flash we need to get the attached torch channel */
  125. if (channel->mode & GB_CHANNEL_MODE_FLASH)
  126. channel = get_channel_from_mode(channel->light,
  127. GB_CHANNEL_MODE_TORCH);
  128. /* For not flash we need to convert brightness to intensity */
  129. intensity = channel->intensity_uA.min +
  130. (channel->intensity_uA.step * channel->led->brightness);
  131. return __gb_lights_flash_intensity_set(channel, intensity);
  132. }
  133. #else
  134. static struct gb_channel *get_channel_from_cdev(struct led_classdev *cdev)
  135. {
  136. return container_of(cdev, struct gb_channel, cled);
  137. }
  138. static struct led_classdev *get_channel_cdev(struct gb_channel *channel)
  139. {
  140. return &channel->cled;
  141. }
  142. static int __gb_lights_flash_brightness_set(struct gb_channel *channel)
  143. {
  144. return 0;
  145. }
  146. #endif
  147. static int gb_lights_color_set(struct gb_channel *channel, u32 color);
  148. static int gb_lights_fade_set(struct gb_channel *channel);
  149. static void led_lock(struct led_classdev *cdev)
  150. {
  151. mutex_lock(&cdev->led_access);
  152. }
  153. static void led_unlock(struct led_classdev *cdev)
  154. {
  155. mutex_unlock(&cdev->led_access);
  156. }
  157. #define gb_lights_fade_attr(__dir) \
  158. static ssize_t fade_##__dir##_show(struct device *dev, \
  159. struct device_attribute *attr, \
  160. char *buf) \
  161. { \
  162. struct led_classdev *cdev = dev_get_drvdata(dev); \
  163. struct gb_channel *channel = get_channel_from_cdev(cdev); \
  164. \
  165. return sprintf(buf, "%u\n", channel->fade_##__dir); \
  166. } \
  167. \
  168. static ssize_t fade_##__dir##_store(struct device *dev, \
  169. struct device_attribute *attr, \
  170. const char *buf, size_t size) \
  171. { \
  172. struct led_classdev *cdev = dev_get_drvdata(dev); \
  173. struct gb_channel *channel = get_channel_from_cdev(cdev); \
  174. u8 fade; \
  175. int ret; \
  176. \
  177. led_lock(cdev); \
  178. if (led_sysfs_is_disabled(cdev)) { \
  179. ret = -EBUSY; \
  180. goto unlock; \
  181. } \
  182. \
  183. ret = kstrtou8(buf, 0, &fade); \
  184. if (ret < 0) { \
  185. dev_err(dev, "could not parse fade value %d\n", ret); \
  186. goto unlock; \
  187. } \
  188. if (channel->fade_##__dir == fade) \
  189. goto unlock; \
  190. channel->fade_##__dir = fade; \
  191. \
  192. ret = gb_lights_fade_set(channel); \
  193. if (ret < 0) \
  194. goto unlock; \
  195. \
  196. ret = size; \
  197. unlock: \
  198. led_unlock(cdev); \
  199. return ret; \
  200. } \
  201. static DEVICE_ATTR_RW(fade_##__dir)
  202. gb_lights_fade_attr(in);
  203. gb_lights_fade_attr(out);
  204. static ssize_t color_show(struct device *dev, struct device_attribute *attr,
  205. char *buf)
  206. {
  207. struct led_classdev *cdev = dev_get_drvdata(dev);
  208. struct gb_channel *channel = get_channel_from_cdev(cdev);
  209. return sprintf(buf, "0x%08x\n", channel->color);
  210. }
  211. static ssize_t color_store(struct device *dev, struct device_attribute *attr,
  212. const char *buf, size_t size)
  213. {
  214. struct led_classdev *cdev = dev_get_drvdata(dev);
  215. struct gb_channel *channel = get_channel_from_cdev(cdev);
  216. u32 color;
  217. int ret;
  218. led_lock(cdev);
  219. if (led_sysfs_is_disabled(cdev)) {
  220. ret = -EBUSY;
  221. goto unlock;
  222. }
  223. ret = kstrtou32(buf, 0, &color);
  224. if (ret < 0) {
  225. dev_err(dev, "could not parse color value %d\n", ret);
  226. goto unlock;
  227. }
  228. ret = gb_lights_color_set(channel, color);
  229. if (ret < 0)
  230. goto unlock;
  231. channel->color = color;
  232. ret = size;
  233. unlock:
  234. led_unlock(cdev);
  235. return ret;
  236. }
  237. static DEVICE_ATTR_RW(color);
  238. static int channel_attr_groups_set(struct gb_channel *channel,
  239. struct led_classdev *cdev)
  240. {
  241. int attr = 0;
  242. int size = 0;
  243. if (channel->flags & GB_LIGHT_CHANNEL_MULTICOLOR)
  244. size++;
  245. if (channel->flags & GB_LIGHT_CHANNEL_FADER)
  246. size += 2;
  247. if (!size)
  248. return 0;
  249. /* Set attributes based in the channel flags */
  250. channel->attrs = kcalloc(size + 1, sizeof(*channel->attrs), GFP_KERNEL);
  251. if (!channel->attrs)
  252. return -ENOMEM;
  253. channel->attr_group = kcalloc(1, sizeof(*channel->attr_group),
  254. GFP_KERNEL);
  255. if (!channel->attr_group)
  256. return -ENOMEM;
  257. channel->attr_groups = kcalloc(2, sizeof(*channel->attr_groups),
  258. GFP_KERNEL);
  259. if (!channel->attr_groups)
  260. return -ENOMEM;
  261. if (channel->flags & GB_LIGHT_CHANNEL_MULTICOLOR)
  262. channel->attrs[attr++] = &dev_attr_color.attr;
  263. if (channel->flags & GB_LIGHT_CHANNEL_FADER) {
  264. channel->attrs[attr++] = &dev_attr_fade_in.attr;
  265. channel->attrs[attr++] = &dev_attr_fade_out.attr;
  266. }
  267. channel->attr_group->attrs = channel->attrs;
  268. channel->attr_groups[0] = channel->attr_group;
  269. cdev->groups = channel->attr_groups;
  270. return 0;
  271. }
  272. static int gb_lights_fade_set(struct gb_channel *channel)
  273. {
  274. struct gb_connection *connection = get_conn_from_channel(channel);
  275. struct gb_bundle *bundle = connection->bundle;
  276. struct gb_lights_set_fade_request req;
  277. int ret;
  278. if (channel->releasing)
  279. return -ESHUTDOWN;
  280. ret = gb_pm_runtime_get_sync(bundle);
  281. if (ret < 0)
  282. return ret;
  283. req.light_id = channel->light->id;
  284. req.channel_id = channel->id;
  285. req.fade_in = channel->fade_in;
  286. req.fade_out = channel->fade_out;
  287. ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_FADE,
  288. &req, sizeof(req), NULL, 0);
  289. gb_pm_runtime_put_autosuspend(bundle);
  290. return ret;
  291. }
  292. static int gb_lights_color_set(struct gb_channel *channel, u32 color)
  293. {
  294. struct gb_connection *connection = get_conn_from_channel(channel);
  295. struct gb_bundle *bundle = connection->bundle;
  296. struct gb_lights_set_color_request req;
  297. int ret;
  298. if (channel->releasing)
  299. return -ESHUTDOWN;
  300. ret = gb_pm_runtime_get_sync(bundle);
  301. if (ret < 0)
  302. return ret;
  303. req.light_id = channel->light->id;
  304. req.channel_id = channel->id;
  305. req.color = cpu_to_le32(color);
  306. ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_COLOR,
  307. &req, sizeof(req), NULL, 0);
  308. gb_pm_runtime_put_autosuspend(bundle);
  309. return ret;
  310. }
  311. static int __gb_lights_led_brightness_set(struct gb_channel *channel)
  312. {
  313. struct gb_lights_set_brightness_request req;
  314. struct gb_connection *connection = get_conn_from_channel(channel);
  315. struct gb_bundle *bundle = connection->bundle;
  316. bool old_active;
  317. int ret;
  318. mutex_lock(&channel->lock);
  319. ret = gb_pm_runtime_get_sync(bundle);
  320. if (ret < 0)
  321. goto out_unlock;
  322. old_active = channel->active;
  323. req.light_id = channel->light->id;
  324. req.channel_id = channel->id;
  325. req.brightness = (u8)channel->led->brightness;
  326. ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_BRIGHTNESS,
  327. &req, sizeof(req), NULL, 0);
  328. if (ret < 0)
  329. goto out_pm_put;
  330. if (channel->led->brightness)
  331. channel->active = true;
  332. else
  333. channel->active = false;
  334. /* we need to keep module alive when turning to active state */
  335. if (!old_active && channel->active)
  336. goto out_unlock;
  337. /*
  338. * on the other hand if going to inactive we still hold a reference and
  339. * need to put it, so we could go to suspend.
  340. */
  341. if (old_active && !channel->active)
  342. gb_pm_runtime_put_autosuspend(bundle);
  343. out_pm_put:
  344. gb_pm_runtime_put_autosuspend(bundle);
  345. out_unlock:
  346. mutex_unlock(&channel->lock);
  347. return ret;
  348. }
  349. static int __gb_lights_brightness_set(struct gb_channel *channel)
  350. {
  351. int ret;
  352. if (channel->releasing)
  353. return 0;
  354. if (is_channel_flash(channel))
  355. ret = __gb_lights_flash_brightness_set(channel);
  356. else
  357. ret = __gb_lights_led_brightness_set(channel);
  358. return ret;
  359. }
  360. static int gb_brightness_set(struct led_classdev *cdev,
  361. enum led_brightness value)
  362. {
  363. struct gb_channel *channel = get_channel_from_cdev(cdev);
  364. channel->led->brightness = value;
  365. return __gb_lights_brightness_set(channel);
  366. }
  367. static enum led_brightness gb_brightness_get(struct led_classdev *cdev)
  368. {
  369. struct gb_channel *channel = get_channel_from_cdev(cdev);
  370. return channel->led->brightness;
  371. }
  372. static int gb_blink_set(struct led_classdev *cdev, unsigned long *delay_on,
  373. unsigned long *delay_off)
  374. {
  375. struct gb_channel *channel = get_channel_from_cdev(cdev);
  376. struct gb_connection *connection = get_conn_from_channel(channel);
  377. struct gb_bundle *bundle = connection->bundle;
  378. struct gb_lights_blink_request req;
  379. bool old_active;
  380. int ret;
  381. if (channel->releasing)
  382. return -ESHUTDOWN;
  383. if (!delay_on || !delay_off)
  384. return -EINVAL;
  385. mutex_lock(&channel->lock);
  386. ret = gb_pm_runtime_get_sync(bundle);
  387. if (ret < 0)
  388. goto out_unlock;
  389. old_active = channel->active;
  390. req.light_id = channel->light->id;
  391. req.channel_id = channel->id;
  392. req.time_on_ms = cpu_to_le16(*delay_on);
  393. req.time_off_ms = cpu_to_le16(*delay_off);
  394. ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_BLINK, &req,
  395. sizeof(req), NULL, 0);
  396. if (ret < 0)
  397. goto out_pm_put;
  398. if (*delay_on)
  399. channel->active = true;
  400. else
  401. channel->active = false;
  402. /* we need to keep module alive when turning to active state */
  403. if (!old_active && channel->active)
  404. goto out_unlock;
  405. /*
  406. * on the other hand if going to inactive we still hold a reference and
  407. * need to put it, so we could go to suspend.
  408. */
  409. if (old_active && !channel->active)
  410. gb_pm_runtime_put_autosuspend(bundle);
  411. out_pm_put:
  412. gb_pm_runtime_put_autosuspend(bundle);
  413. out_unlock:
  414. mutex_unlock(&channel->lock);
  415. return ret;
  416. }
  417. static void gb_lights_led_operations_set(struct gb_channel *channel,
  418. struct led_classdev *cdev)
  419. {
  420. cdev->brightness_get = gb_brightness_get;
  421. cdev->brightness_set_blocking = gb_brightness_set;
  422. if (channel->flags & GB_LIGHT_CHANNEL_BLINK)
  423. cdev->blink_set = gb_blink_set;
  424. }
  425. #if IS_REACHABLE(CONFIG_V4L2_FLASH_LED_CLASS)
  426. /* V4L2 specific helpers */
  427. static const struct v4l2_flash_ops v4l2_flash_ops;
  428. static void __gb_lights_channel_v4l2_config(struct led_flash_setting *channel_s,
  429. struct led_flash_setting *v4l2_s)
  430. {
  431. v4l2_s->min = channel_s->min;
  432. v4l2_s->max = channel_s->max;
  433. v4l2_s->step = channel_s->step;
  434. /* For v4l2 val is the default value */
  435. v4l2_s->val = channel_s->max;
  436. }
  437. static int gb_lights_light_v4l2_register(struct gb_light *light)
  438. {
  439. struct gb_connection *connection = get_conn_from_light(light);
  440. struct device *dev = &connection->bundle->dev;
  441. struct v4l2_flash_config *sd_cfg;
  442. struct led_classdev_flash *fled;
  443. struct led_classdev_flash *iled = NULL;
  444. struct gb_channel *channel_torch, *channel_ind, *channel_flash;
  445. int ret = 0;
  446. sd_cfg = kcalloc(1, sizeof(*sd_cfg), GFP_KERNEL);
  447. if (!sd_cfg)
  448. return -ENOMEM;
  449. channel_torch = get_channel_from_mode(light, GB_CHANNEL_MODE_TORCH);
  450. if (channel_torch)
  451. __gb_lights_channel_v4l2_config(&channel_torch->intensity_uA,
  452. &sd_cfg->torch_intensity);
  453. channel_ind = get_channel_from_mode(light, GB_CHANNEL_MODE_INDICATOR);
  454. if (channel_ind) {
  455. __gb_lights_channel_v4l2_config(&channel_ind->intensity_uA,
  456. &sd_cfg->indicator_intensity);
  457. iled = &channel_ind->fled;
  458. }
  459. channel_flash = get_channel_from_mode(light, GB_CHANNEL_MODE_FLASH);
  460. WARN_ON(!channel_flash);
  461. fled = &channel_flash->fled;
  462. snprintf(sd_cfg->dev_name, sizeof(sd_cfg->dev_name), "%s", light->name);
  463. /* Set the possible values to faults, in our case all faults */
  464. sd_cfg->flash_faults = LED_FAULT_OVER_VOLTAGE | LED_FAULT_TIMEOUT |
  465. LED_FAULT_OVER_TEMPERATURE | LED_FAULT_SHORT_CIRCUIT |
  466. LED_FAULT_OVER_CURRENT | LED_FAULT_INDICATOR |
  467. LED_FAULT_UNDER_VOLTAGE | LED_FAULT_INPUT_VOLTAGE |
  468. LED_FAULT_LED_OVER_TEMPERATURE;
  469. light->v4l2_flash = v4l2_flash_init(dev, NULL, fled, iled,
  470. &v4l2_flash_ops, sd_cfg);
  471. if (IS_ERR_OR_NULL(light->v4l2_flash)) {
  472. ret = PTR_ERR(light->v4l2_flash);
  473. goto out_free;
  474. }
  475. return ret;
  476. out_free:
  477. kfree(sd_cfg);
  478. return ret;
  479. }
  480. static void gb_lights_light_v4l2_unregister(struct gb_light *light)
  481. {
  482. v4l2_flash_release(light->v4l2_flash);
  483. }
  484. #else
  485. static int gb_lights_light_v4l2_register(struct gb_light *light)
  486. {
  487. struct gb_connection *connection = get_conn_from_light(light);
  488. dev_err(&connection->bundle->dev, "no support for v4l2 subdevices\n");
  489. return 0;
  490. }
  491. static void gb_lights_light_v4l2_unregister(struct gb_light *light)
  492. {
  493. }
  494. #endif
  495. #if IS_REACHABLE(CONFIG_LEDS_CLASS_FLASH)
  496. /* Flash specific operations */
  497. static int gb_lights_flash_intensity_set(struct led_classdev_flash *fcdev,
  498. u32 brightness)
  499. {
  500. struct gb_channel *channel = container_of(fcdev, struct gb_channel,
  501. fled);
  502. int ret;
  503. ret = __gb_lights_flash_intensity_set(channel, brightness);
  504. if (ret < 0)
  505. return ret;
  506. fcdev->brightness.val = brightness;
  507. return 0;
  508. }
  509. static int gb_lights_flash_intensity_get(struct led_classdev_flash *fcdev,
  510. u32 *brightness)
  511. {
  512. *brightness = fcdev->brightness.val;
  513. return 0;
  514. }
  515. static int gb_lights_flash_strobe_set(struct led_classdev_flash *fcdev,
  516. bool state)
  517. {
  518. struct gb_channel *channel = container_of(fcdev, struct gb_channel,
  519. fled);
  520. struct gb_connection *connection = get_conn_from_channel(channel);
  521. struct gb_bundle *bundle = connection->bundle;
  522. struct gb_lights_set_flash_strobe_request req;
  523. int ret;
  524. if (channel->releasing)
  525. return -ESHUTDOWN;
  526. ret = gb_pm_runtime_get_sync(bundle);
  527. if (ret < 0)
  528. return ret;
  529. req.light_id = channel->light->id;
  530. req.channel_id = channel->id;
  531. req.state = state ? 1 : 0;
  532. ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_FLASH_STROBE,
  533. &req, sizeof(req), NULL, 0);
  534. if (!ret)
  535. channel->strobe_state = state;
  536. gb_pm_runtime_put_autosuspend(bundle);
  537. return ret;
  538. }
  539. static int gb_lights_flash_strobe_get(struct led_classdev_flash *fcdev,
  540. bool *state)
  541. {
  542. struct gb_channel *channel = container_of(fcdev, struct gb_channel,
  543. fled);
  544. *state = channel->strobe_state;
  545. return 0;
  546. }
  547. static int gb_lights_flash_timeout_set(struct led_classdev_flash *fcdev,
  548. u32 timeout)
  549. {
  550. struct gb_channel *channel = container_of(fcdev, struct gb_channel,
  551. fled);
  552. struct gb_connection *connection = get_conn_from_channel(channel);
  553. struct gb_bundle *bundle = connection->bundle;
  554. struct gb_lights_set_flash_timeout_request req;
  555. int ret;
  556. if (channel->releasing)
  557. return -ESHUTDOWN;
  558. ret = gb_pm_runtime_get_sync(bundle);
  559. if (ret < 0)
  560. return ret;
  561. req.light_id = channel->light->id;
  562. req.channel_id = channel->id;
  563. req.timeout_us = cpu_to_le32(timeout);
  564. ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_FLASH_TIMEOUT,
  565. &req, sizeof(req), NULL, 0);
  566. if (!ret)
  567. fcdev->timeout.val = timeout;
  568. gb_pm_runtime_put_autosuspend(bundle);
  569. return ret;
  570. }
  571. static int gb_lights_flash_fault_get(struct led_classdev_flash *fcdev,
  572. u32 *fault)
  573. {
  574. struct gb_channel *channel = container_of(fcdev, struct gb_channel,
  575. fled);
  576. struct gb_connection *connection = get_conn_from_channel(channel);
  577. struct gb_bundle *bundle = connection->bundle;
  578. struct gb_lights_get_flash_fault_request req;
  579. struct gb_lights_get_flash_fault_response resp;
  580. int ret;
  581. if (channel->releasing)
  582. return -ESHUTDOWN;
  583. ret = gb_pm_runtime_get_sync(bundle);
  584. if (ret < 0)
  585. return ret;
  586. req.light_id = channel->light->id;
  587. req.channel_id = channel->id;
  588. ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_GET_FLASH_FAULT,
  589. &req, sizeof(req), &resp, sizeof(resp));
  590. if (!ret)
  591. *fault = le32_to_cpu(resp.fault);
  592. gb_pm_runtime_put_autosuspend(bundle);
  593. return ret;
  594. }
  595. static const struct led_flash_ops gb_lights_flash_ops = {
  596. .flash_brightness_set = gb_lights_flash_intensity_set,
  597. .flash_brightness_get = gb_lights_flash_intensity_get,
  598. .strobe_set = gb_lights_flash_strobe_set,
  599. .strobe_get = gb_lights_flash_strobe_get,
  600. .timeout_set = gb_lights_flash_timeout_set,
  601. .fault_get = gb_lights_flash_fault_get,
  602. };
  603. static int __gb_lights_channel_torch_attach(struct gb_channel *channel,
  604. struct gb_channel *channel_torch)
  605. {
  606. char *name;
  607. /* we can only attach torch to a flash channel */
  608. if (!(channel->mode & GB_CHANNEL_MODE_FLASH))
  609. return 0;
  610. /* Move torch brightness to the destination */
  611. channel->led->max_brightness = channel_torch->led->max_brightness;
  612. /* append mode name to flash name */
  613. name = kasprintf(GFP_KERNEL, "%s_%s", channel->led->name,
  614. channel_torch->mode_name);
  615. if (!name)
  616. return -ENOMEM;
  617. kfree(channel->led->name);
  618. channel->led->name = name;
  619. channel_torch->led = channel->led;
  620. return 0;
  621. }
  622. static int __gb_lights_flash_led_register(struct gb_channel *channel)
  623. {
  624. struct gb_connection *connection = get_conn_from_channel(channel);
  625. struct led_classdev_flash *fled = &channel->fled;
  626. struct led_flash_setting *fset;
  627. struct gb_channel *channel_torch;
  628. int ret;
  629. fled->ops = &gb_lights_flash_ops;
  630. fled->led_cdev.flags |= LED_DEV_CAP_FLASH;
  631. fset = &fled->brightness;
  632. fset->min = channel->intensity_uA.min;
  633. fset->max = channel->intensity_uA.max;
  634. fset->step = channel->intensity_uA.step;
  635. fset->val = channel->intensity_uA.max;
  636. /* Only the flash mode have the timeout constraints settings */
  637. if (channel->mode & GB_CHANNEL_MODE_FLASH) {
  638. fset = &fled->timeout;
  639. fset->min = channel->timeout_us.min;
  640. fset->max = channel->timeout_us.max;
  641. fset->step = channel->timeout_us.step;
  642. fset->val = channel->timeout_us.max;
  643. }
  644. /*
  645. * If light have torch mode channel, this channel will be the led
  646. * classdev of the registered above flash classdev
  647. */
  648. channel_torch = get_channel_from_mode(channel->light,
  649. GB_CHANNEL_MODE_TORCH);
  650. if (channel_torch) {
  651. ret = __gb_lights_channel_torch_attach(channel, channel_torch);
  652. if (ret < 0)
  653. goto fail;
  654. }
  655. ret = led_classdev_flash_register(&connection->bundle->dev, fled);
  656. if (ret < 0)
  657. goto fail;
  658. channel->is_registered = true;
  659. return 0;
  660. fail:
  661. channel->led = NULL;
  662. return ret;
  663. }
  664. static void __gb_lights_flash_led_unregister(struct gb_channel *channel)
  665. {
  666. if (!channel->is_registered)
  667. return;
  668. led_classdev_flash_unregister(&channel->fled);
  669. }
  670. static int gb_lights_channel_flash_config(struct gb_channel *channel)
  671. {
  672. struct gb_connection *connection = get_conn_from_channel(channel);
  673. struct gb_lights_get_channel_flash_config_request req;
  674. struct gb_lights_get_channel_flash_config_response conf;
  675. struct led_flash_setting *fset;
  676. int ret;
  677. req.light_id = channel->light->id;
  678. req.channel_id = channel->id;
  679. ret = gb_operation_sync(connection,
  680. GB_LIGHTS_TYPE_GET_CHANNEL_FLASH_CONFIG,
  681. &req, sizeof(req), &conf, sizeof(conf));
  682. if (ret < 0)
  683. return ret;
  684. /*
  685. * Intensity constraints for flash related modes: flash, torch,
  686. * indicator. They will be needed for v4l2 registration.
  687. */
  688. fset = &channel->intensity_uA;
  689. fset->min = le32_to_cpu(conf.intensity_min_uA);
  690. fset->max = le32_to_cpu(conf.intensity_max_uA);
  691. fset->step = le32_to_cpu(conf.intensity_step_uA);
  692. /*
  693. * On flash type, max brightness is set as the number of intensity steps
  694. * available.
  695. */
  696. channel->led->max_brightness = (fset->max - fset->min) / fset->step;
  697. /* Only the flash mode have the timeout constraints settings */
  698. if (channel->mode & GB_CHANNEL_MODE_FLASH) {
  699. fset = &channel->timeout_us;
  700. fset->min = le32_to_cpu(conf.timeout_min_us);
  701. fset->max = le32_to_cpu(conf.timeout_max_us);
  702. fset->step = le32_to_cpu(conf.timeout_step_us);
  703. }
  704. return 0;
  705. }
  706. #else
  707. static int gb_lights_channel_flash_config(struct gb_channel *channel)
  708. {
  709. struct gb_connection *connection = get_conn_from_channel(channel);
  710. dev_err(&connection->bundle->dev, "no support for flash devices\n");
  711. return 0;
  712. }
  713. static int __gb_lights_flash_led_register(struct gb_channel *channel)
  714. {
  715. return 0;
  716. }
  717. static void __gb_lights_flash_led_unregister(struct gb_channel *channel)
  718. {
  719. }
  720. #endif
  721. static int __gb_lights_led_register(struct gb_channel *channel)
  722. {
  723. struct gb_connection *connection = get_conn_from_channel(channel);
  724. struct led_classdev *cdev = get_channel_cdev(channel);
  725. int ret;
  726. ret = led_classdev_register(&connection->bundle->dev, cdev);
  727. if (ret < 0)
  728. channel->led = NULL;
  729. else
  730. channel->is_registered = true;
  731. return ret;
  732. }
  733. static int gb_lights_channel_register(struct gb_channel *channel)
  734. {
  735. /* Normal LED channel, just register in led classdev and we are done */
  736. if (!is_channel_flash(channel))
  737. return __gb_lights_led_register(channel);
  738. /*
  739. * Flash Type need more work, register flash classdev, indicator as
  740. * flash classdev, torch will be led classdev of the flash classdev.
  741. */
  742. if (!(channel->mode & GB_CHANNEL_MODE_TORCH))
  743. return __gb_lights_flash_led_register(channel);
  744. return 0;
  745. }
  746. static void __gb_lights_led_unregister(struct gb_channel *channel)
  747. {
  748. struct led_classdev *cdev = get_channel_cdev(channel);
  749. if (!channel->is_registered)
  750. return;
  751. led_classdev_unregister(cdev);
  752. kfree(cdev->name);
  753. cdev->name = NULL;
  754. channel->led = NULL;
  755. }
  756. static void gb_lights_channel_unregister(struct gb_channel *channel)
  757. {
  758. /* The same as register, handle channels differently */
  759. if (!is_channel_flash(channel)) {
  760. __gb_lights_led_unregister(channel);
  761. return;
  762. }
  763. if (channel->mode & GB_CHANNEL_MODE_TORCH)
  764. __gb_lights_led_unregister(channel);
  765. else
  766. __gb_lights_flash_led_unregister(channel);
  767. }
  768. static int gb_lights_channel_config(struct gb_light *light,
  769. struct gb_channel *channel)
  770. {
  771. struct gb_lights_get_channel_config_response conf;
  772. struct gb_lights_get_channel_config_request req;
  773. struct gb_connection *connection = get_conn_from_light(light);
  774. struct led_classdev *cdev = get_channel_cdev(channel);
  775. char *name;
  776. int ret;
  777. req.light_id = light->id;
  778. req.channel_id = channel->id;
  779. ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_GET_CHANNEL_CONFIG,
  780. &req, sizeof(req), &conf, sizeof(conf));
  781. if (ret < 0)
  782. return ret;
  783. channel->light = light;
  784. channel->mode = le32_to_cpu(conf.mode);
  785. channel->flags = le32_to_cpu(conf.flags);
  786. channel->color = le32_to_cpu(conf.color);
  787. channel->color_name = kstrndup(conf.color_name, NAMES_MAX, GFP_KERNEL);
  788. if (!channel->color_name)
  789. return -ENOMEM;
  790. channel->mode_name = kstrndup(conf.mode_name, NAMES_MAX, GFP_KERNEL);
  791. if (!channel->mode_name)
  792. return -ENOMEM;
  793. channel->led = cdev;
  794. name = kasprintf(GFP_KERNEL, "%s:%s:%s", light->name,
  795. channel->color_name, channel->mode_name);
  796. if (!name)
  797. return -ENOMEM;
  798. cdev->name = name;
  799. cdev->max_brightness = conf.max_brightness;
  800. ret = channel_attr_groups_set(channel, cdev);
  801. if (ret < 0)
  802. return ret;
  803. gb_lights_led_operations_set(channel, cdev);
  804. /*
  805. * If it is not a flash related channel (flash, torch or indicator) we
  806. * are done here. If not, continue and fetch flash related
  807. * configurations.
  808. */
  809. if (!is_channel_flash(channel))
  810. return ret;
  811. light->has_flash = true;
  812. ret = gb_lights_channel_flash_config(channel);
  813. if (ret < 0)
  814. return ret;
  815. return ret;
  816. }
  817. static int gb_lights_light_config(struct gb_lights *glights, u8 id)
  818. {
  819. struct gb_light *light = &glights->lights[id];
  820. struct gb_lights_get_light_config_request req;
  821. struct gb_lights_get_light_config_response conf;
  822. int ret;
  823. int i;
  824. light->glights = glights;
  825. light->id = id;
  826. req.id = id;
  827. ret = gb_operation_sync(glights->connection,
  828. GB_LIGHTS_TYPE_GET_LIGHT_CONFIG,
  829. &req, sizeof(req), &conf, sizeof(conf));
  830. if (ret < 0)
  831. return ret;
  832. if (!conf.channel_count)
  833. return -EINVAL;
  834. if (!strlen(conf.name))
  835. return -EINVAL;
  836. light->channels_count = conf.channel_count;
  837. light->name = kstrndup(conf.name, NAMES_MAX, GFP_KERNEL);
  838. light->channels = kzalloc(light->channels_count *
  839. sizeof(struct gb_channel), GFP_KERNEL);
  840. if (!light->channels)
  841. return -ENOMEM;
  842. /* First we collect all the configurations for all channels */
  843. for (i = 0; i < light->channels_count; i++) {
  844. light->channels[i].id = i;
  845. ret = gb_lights_channel_config(light, &light->channels[i]);
  846. if (ret < 0)
  847. return ret;
  848. }
  849. return 0;
  850. }
  851. static int gb_lights_light_register(struct gb_light *light)
  852. {
  853. int ret;
  854. int i;
  855. /*
  856. * Then, if everything went ok in getting configurations, we register
  857. * the classdev, flash classdev and v4l2 subsystem, if a flash device is
  858. * found.
  859. */
  860. for (i = 0; i < light->channels_count; i++) {
  861. ret = gb_lights_channel_register(&light->channels[i]);
  862. if (ret < 0)
  863. return ret;
  864. mutex_init(&light->channels[i].lock);
  865. }
  866. light->ready = true;
  867. if (light->has_flash) {
  868. ret = gb_lights_light_v4l2_register(light);
  869. if (ret < 0) {
  870. light->has_flash = false;
  871. return ret;
  872. }
  873. }
  874. return 0;
  875. }
  876. static void gb_lights_channel_free(struct gb_channel *channel)
  877. {
  878. kfree(channel->attrs);
  879. kfree(channel->attr_group);
  880. kfree(channel->attr_groups);
  881. kfree(channel->color_name);
  882. kfree(channel->mode_name);
  883. mutex_destroy(&channel->lock);
  884. }
  885. static void gb_lights_channel_release(struct gb_channel *channel)
  886. {
  887. channel->releasing = true;
  888. gb_lights_channel_unregister(channel);
  889. gb_lights_channel_free(channel);
  890. }
  891. static void gb_lights_light_release(struct gb_light *light)
  892. {
  893. int i;
  894. int count;
  895. light->ready = false;
  896. count = light->channels_count;
  897. if (light->has_flash)
  898. gb_lights_light_v4l2_unregister(light);
  899. for (i = 0; i < count; i++) {
  900. gb_lights_channel_release(&light->channels[i]);
  901. light->channels_count--;
  902. }
  903. kfree(light->channels);
  904. kfree(light->name);
  905. }
  906. static void gb_lights_release(struct gb_lights *glights)
  907. {
  908. int i;
  909. if (!glights)
  910. return;
  911. mutex_lock(&glights->lights_lock);
  912. if (!glights->lights)
  913. goto free_glights;
  914. for (i = 0; i < glights->lights_count; i++)
  915. gb_lights_light_release(&glights->lights[i]);
  916. kfree(glights->lights);
  917. free_glights:
  918. mutex_unlock(&glights->lights_lock);
  919. mutex_destroy(&glights->lights_lock);
  920. kfree(glights);
  921. }
  922. static int gb_lights_get_count(struct gb_lights *glights)
  923. {
  924. struct gb_lights_get_lights_response resp;
  925. int ret;
  926. ret = gb_operation_sync(glights->connection, GB_LIGHTS_TYPE_GET_LIGHTS,
  927. NULL, 0, &resp, sizeof(resp));
  928. if (ret < 0)
  929. return ret;
  930. if (!resp.lights_count)
  931. return -EINVAL;
  932. glights->lights_count = resp.lights_count;
  933. return 0;
  934. }
  935. static int gb_lights_create_all(struct gb_lights *glights)
  936. {
  937. struct gb_connection *connection = glights->connection;
  938. int ret;
  939. int i;
  940. mutex_lock(&glights->lights_lock);
  941. ret = gb_lights_get_count(glights);
  942. if (ret < 0)
  943. goto out;
  944. glights->lights = kzalloc(glights->lights_count *
  945. sizeof(struct gb_light), GFP_KERNEL);
  946. if (!glights->lights) {
  947. ret = -ENOMEM;
  948. goto out;
  949. }
  950. for (i = 0; i < glights->lights_count; i++) {
  951. ret = gb_lights_light_config(glights, i);
  952. if (ret < 0) {
  953. dev_err(&connection->bundle->dev,
  954. "Fail to configure lights device\n");
  955. goto out;
  956. }
  957. }
  958. out:
  959. mutex_unlock(&glights->lights_lock);
  960. return ret;
  961. }
  962. static int gb_lights_register_all(struct gb_lights *glights)
  963. {
  964. struct gb_connection *connection = glights->connection;
  965. int ret = 0;
  966. int i;
  967. mutex_lock(&glights->lights_lock);
  968. for (i = 0; i < glights->lights_count; i++) {
  969. ret = gb_lights_light_register(&glights->lights[i]);
  970. if (ret < 0) {
  971. dev_err(&connection->bundle->dev,
  972. "Fail to enable lights device\n");
  973. break;
  974. }
  975. }
  976. mutex_unlock(&glights->lights_lock);
  977. return ret;
  978. }
  979. static int gb_lights_request_handler(struct gb_operation *op)
  980. {
  981. struct gb_connection *connection = op->connection;
  982. struct device *dev = &connection->bundle->dev;
  983. struct gb_lights *glights = gb_connection_get_data(connection);
  984. struct gb_light *light;
  985. struct gb_message *request;
  986. struct gb_lights_event_request *payload;
  987. int ret = 0;
  988. u8 light_id;
  989. u8 event;
  990. if (op->type != GB_LIGHTS_TYPE_EVENT) {
  991. dev_err(dev, "Unsupported unsolicited event: %u\n", op->type);
  992. return -EINVAL;
  993. }
  994. request = op->request;
  995. if (request->payload_size < sizeof(*payload)) {
  996. dev_err(dev, "Wrong event size received (%zu < %zu)\n",
  997. request->payload_size, sizeof(*payload));
  998. return -EINVAL;
  999. }
  1000. payload = request->payload;
  1001. light_id = payload->light_id;
  1002. if (light_id >= glights->lights_count ||
  1003. !glights->lights[light_id].ready) {
  1004. dev_err(dev, "Event received for unconfigured light id: %d\n",
  1005. light_id);
  1006. return -EINVAL;
  1007. }
  1008. event = payload->event;
  1009. if (event & GB_LIGHTS_LIGHT_CONFIG) {
  1010. light = &glights->lights[light_id];
  1011. mutex_lock(&glights->lights_lock);
  1012. gb_lights_light_release(light);
  1013. ret = gb_lights_light_config(glights, light_id);
  1014. if (!ret)
  1015. ret = gb_lights_light_register(light);
  1016. if (ret < 0)
  1017. gb_lights_light_release(light);
  1018. mutex_unlock(&glights->lights_lock);
  1019. }
  1020. return ret;
  1021. }
  1022. static int gb_lights_probe(struct gb_bundle *bundle,
  1023. const struct greybus_bundle_id *id)
  1024. {
  1025. struct greybus_descriptor_cport *cport_desc;
  1026. struct gb_connection *connection;
  1027. struct gb_lights *glights;
  1028. int ret;
  1029. if (bundle->num_cports != 1)
  1030. return -ENODEV;
  1031. cport_desc = &bundle->cport_desc[0];
  1032. if (cport_desc->protocol_id != GREYBUS_PROTOCOL_LIGHTS)
  1033. return -ENODEV;
  1034. glights = kzalloc(sizeof(*glights), GFP_KERNEL);
  1035. if (!glights)
  1036. return -ENOMEM;
  1037. mutex_init(&glights->lights_lock);
  1038. connection = gb_connection_create(bundle, le16_to_cpu(cport_desc->id),
  1039. gb_lights_request_handler);
  1040. if (IS_ERR(connection)) {
  1041. ret = PTR_ERR(connection);
  1042. goto out;
  1043. }
  1044. glights->connection = connection;
  1045. gb_connection_set_data(connection, glights);
  1046. greybus_set_drvdata(bundle, glights);
  1047. /* We aren't ready to receive an incoming request yet */
  1048. ret = gb_connection_enable_tx(connection);
  1049. if (ret)
  1050. goto error_connection_destroy;
  1051. /*
  1052. * Setup all the lights devices over this connection, if anything goes
  1053. * wrong tear down all lights
  1054. */
  1055. ret = gb_lights_create_all(glights);
  1056. if (ret < 0)
  1057. goto error_connection_disable;
  1058. /* We are ready to receive an incoming request now, enable RX as well */
  1059. ret = gb_connection_enable(connection);
  1060. if (ret)
  1061. goto error_connection_disable;
  1062. /* Enable & register lights */
  1063. ret = gb_lights_register_all(glights);
  1064. if (ret < 0)
  1065. goto error_connection_disable;
  1066. gb_pm_runtime_put_autosuspend(bundle);
  1067. return 0;
  1068. error_connection_disable:
  1069. gb_connection_disable(connection);
  1070. error_connection_destroy:
  1071. gb_connection_destroy(connection);
  1072. out:
  1073. gb_lights_release(glights);
  1074. return ret;
  1075. }
  1076. static void gb_lights_disconnect(struct gb_bundle *bundle)
  1077. {
  1078. struct gb_lights *glights = greybus_get_drvdata(bundle);
  1079. if (gb_pm_runtime_get_sync(bundle))
  1080. gb_pm_runtime_get_noresume(bundle);
  1081. gb_connection_disable(glights->connection);
  1082. gb_connection_destroy(glights->connection);
  1083. gb_lights_release(glights);
  1084. }
  1085. static const struct greybus_bundle_id gb_lights_id_table[] = {
  1086. { GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_LIGHTS) },
  1087. { }
  1088. };
  1089. MODULE_DEVICE_TABLE(greybus, gb_lights_id_table);
  1090. static struct greybus_driver gb_lights_driver = {
  1091. .name = "lights",
  1092. .probe = gb_lights_probe,
  1093. .disconnect = gb_lights_disconnect,
  1094. .id_table = gb_lights_id_table,
  1095. };
  1096. module_greybus_driver(gb_lights_driver);
  1097. MODULE_LICENSE("GPL v2");