leds-rt4505.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/bitops.h>
  3. #include <linux/i2c.h>
  4. #include <linux/kernel.h>
  5. #include <linux/led-class-flash.h>
  6. #include <linux/module.h>
  7. #include <linux/mutex.h>
  8. #include <linux/property.h>
  9. #include <linux/regmap.h>
  10. #include <media/v4l2-flash-led-class.h>
  11. #define RT4505_REG_RESET 0x0
  12. #define RT4505_REG_CONFIG 0x8
  13. #define RT4505_REG_ILED 0x9
  14. #define RT4505_REG_ENABLE 0xA
  15. #define RT4505_REG_FLAGS 0xB
  16. #define RT4505_RESET_MASK BIT(7)
  17. #define RT4505_FLASHTO_MASK GENMASK(2, 0)
  18. #define RT4505_ITORCH_MASK GENMASK(7, 5)
  19. #define RT4505_ITORCH_SHIFT 5
  20. #define RT4505_IFLASH_MASK GENMASK(4, 0)
  21. #define RT4505_ENABLE_MASK GENMASK(5, 0)
  22. #define RT4505_TORCH_SET (BIT(0) | BIT(4))
  23. #define RT4505_FLASH_SET (BIT(0) | BIT(1) | BIT(2) | BIT(4))
  24. #define RT4505_EXT_FLASH_SET (BIT(0) | BIT(1) | BIT(4) | BIT(5))
  25. #define RT4505_FLASH_GET (BIT(0) | BIT(1) | BIT(4))
  26. #define RT4505_OVP_MASK BIT(3)
  27. #define RT4505_SHORT_MASK BIT(2)
  28. #define RT4505_OTP_MASK BIT(1)
  29. #define RT4505_TIMEOUT_MASK BIT(0)
  30. #define RT4505_ITORCH_MINUA 46000
  31. #define RT4505_ITORCH_MAXUA 375000
  32. #define RT4505_ITORCH_STPUA 47000
  33. #define RT4505_IFLASH_MINUA 93750
  34. #define RT4505_IFLASH_MAXUA 1500000
  35. #define RT4505_IFLASH_STPUA 93750
  36. #define RT4505_FLASHTO_MINUS 100000
  37. #define RT4505_FLASHTO_MAXUS 800000
  38. #define RT4505_FLASHTO_STPUS 100000
  39. struct rt4505_priv {
  40. struct device *dev;
  41. struct regmap *regmap;
  42. struct mutex lock;
  43. struct led_classdev_flash flash;
  44. struct v4l2_flash *v4l2_flash;
  45. };
  46. static int rt4505_torch_brightness_set(struct led_classdev *lcdev,
  47. enum led_brightness level)
  48. {
  49. struct rt4505_priv *priv =
  50. container_of(lcdev, struct rt4505_priv, flash.led_cdev);
  51. u32 val = 0;
  52. int ret;
  53. mutex_lock(&priv->lock);
  54. if (level != LED_OFF) {
  55. ret = regmap_update_bits(priv->regmap,
  56. RT4505_REG_ILED, RT4505_ITORCH_MASK,
  57. (level - 1) << RT4505_ITORCH_SHIFT);
  58. if (ret)
  59. goto unlock;
  60. val = RT4505_TORCH_SET;
  61. }
  62. ret = regmap_update_bits(priv->regmap, RT4505_REG_ENABLE,
  63. RT4505_ENABLE_MASK, val);
  64. unlock:
  65. mutex_unlock(&priv->lock);
  66. return ret;
  67. }
  68. static enum led_brightness rt4505_torch_brightness_get(
  69. struct led_classdev *lcdev)
  70. {
  71. struct rt4505_priv *priv =
  72. container_of(lcdev, struct rt4505_priv, flash.led_cdev);
  73. u32 val;
  74. int ret;
  75. mutex_lock(&priv->lock);
  76. ret = regmap_read(priv->regmap, RT4505_REG_ENABLE, &val);
  77. if (ret) {
  78. dev_err(lcdev->dev, "Failed to get LED enable\n");
  79. ret = LED_OFF;
  80. goto unlock;
  81. }
  82. if ((val & RT4505_ENABLE_MASK) != RT4505_TORCH_SET) {
  83. ret = LED_OFF;
  84. goto unlock;
  85. }
  86. ret = regmap_read(priv->regmap, RT4505_REG_ILED, &val);
  87. if (ret) {
  88. dev_err(lcdev->dev, "Failed to get LED brightness\n");
  89. ret = LED_OFF;
  90. goto unlock;
  91. }
  92. ret = ((val & RT4505_ITORCH_MASK) >> RT4505_ITORCH_SHIFT) + 1;
  93. unlock:
  94. mutex_unlock(&priv->lock);
  95. return ret;
  96. }
  97. static int rt4505_flash_brightness_set(struct led_classdev_flash *fled_cdev,
  98. u32 brightness)
  99. {
  100. struct rt4505_priv *priv =
  101. container_of(fled_cdev, struct rt4505_priv, flash);
  102. struct led_flash_setting *s = &fled_cdev->brightness;
  103. u32 val = (brightness - s->min) / s->step;
  104. int ret;
  105. mutex_lock(&priv->lock);
  106. ret = regmap_update_bits(priv->regmap, RT4505_REG_ILED,
  107. RT4505_IFLASH_MASK, val);
  108. mutex_unlock(&priv->lock);
  109. return ret;
  110. }
  111. static int rt4505_flash_strobe_set(struct led_classdev_flash *fled_cdev,
  112. bool state)
  113. {
  114. struct rt4505_priv *priv =
  115. container_of(fled_cdev, struct rt4505_priv, flash);
  116. u32 val = state ? RT4505_FLASH_SET : 0;
  117. int ret;
  118. mutex_lock(&priv->lock);
  119. ret = regmap_update_bits(priv->regmap, RT4505_REG_ENABLE,
  120. RT4505_ENABLE_MASK, val);
  121. mutex_unlock(&priv->lock);
  122. return ret;
  123. }
  124. static int rt4505_flash_strobe_get(struct led_classdev_flash *fled_cdev,
  125. bool *state)
  126. {
  127. struct rt4505_priv *priv =
  128. container_of(fled_cdev, struct rt4505_priv, flash);
  129. u32 val;
  130. int ret;
  131. mutex_lock(&priv->lock);
  132. ret = regmap_read(priv->regmap, RT4505_REG_ENABLE, &val);
  133. if (ret)
  134. goto unlock;
  135. *state = (val & RT4505_FLASH_GET) == RT4505_FLASH_GET;
  136. unlock:
  137. mutex_unlock(&priv->lock);
  138. return ret;
  139. }
  140. static int rt4505_flash_timeout_set(struct led_classdev_flash *fled_cdev,
  141. u32 timeout)
  142. {
  143. struct rt4505_priv *priv =
  144. container_of(fled_cdev, struct rt4505_priv, flash);
  145. struct led_flash_setting *s = &fled_cdev->timeout;
  146. u32 val = (timeout - s->min) / s->step;
  147. int ret;
  148. mutex_lock(&priv->lock);
  149. ret = regmap_update_bits(priv->regmap, RT4505_REG_CONFIG,
  150. RT4505_FLASHTO_MASK, val);
  151. mutex_unlock(&priv->lock);
  152. return ret;
  153. }
  154. static int rt4505_fault_get(struct led_classdev_flash *fled_cdev, u32 *fault)
  155. {
  156. struct rt4505_priv *priv =
  157. container_of(fled_cdev, struct rt4505_priv, flash);
  158. u32 val, led_faults = 0;
  159. int ret;
  160. ret = regmap_read(priv->regmap, RT4505_REG_FLAGS, &val);
  161. if (ret)
  162. return ret;
  163. if (val & RT4505_OVP_MASK)
  164. led_faults |= LED_FAULT_OVER_VOLTAGE;
  165. if (val & RT4505_SHORT_MASK)
  166. led_faults |= LED_FAULT_SHORT_CIRCUIT;
  167. if (val & RT4505_OTP_MASK)
  168. led_faults |= LED_FAULT_OVER_TEMPERATURE;
  169. if (val & RT4505_TIMEOUT_MASK)
  170. led_faults |= LED_FAULT_TIMEOUT;
  171. *fault = led_faults;
  172. return 0;
  173. }
  174. static const struct led_flash_ops rt4505_flash_ops = {
  175. .flash_brightness_set = rt4505_flash_brightness_set,
  176. .strobe_set = rt4505_flash_strobe_set,
  177. .strobe_get = rt4505_flash_strobe_get,
  178. .timeout_set = rt4505_flash_timeout_set,
  179. .fault_get = rt4505_fault_get,
  180. };
  181. static bool rt4505_is_accessible_reg(struct device *dev, unsigned int reg)
  182. {
  183. if (reg == RT4505_REG_RESET ||
  184. (reg >= RT4505_REG_CONFIG && reg <= RT4505_REG_FLAGS))
  185. return true;
  186. return false;
  187. }
  188. static const struct regmap_config rt4505_regmap_config = {
  189. .reg_bits = 8,
  190. .val_bits = 8,
  191. .max_register = RT4505_REG_FLAGS,
  192. .readable_reg = rt4505_is_accessible_reg,
  193. .writeable_reg = rt4505_is_accessible_reg,
  194. };
  195. #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS)
  196. static int rt4505_flash_external_strobe_set(struct v4l2_flash *v4l2_flash,
  197. bool enable)
  198. {
  199. struct led_classdev_flash *flash = v4l2_flash->fled_cdev;
  200. struct rt4505_priv *priv =
  201. container_of(flash, struct rt4505_priv, flash);
  202. u32 val = enable ? RT4505_EXT_FLASH_SET : 0;
  203. int ret;
  204. mutex_lock(&priv->lock);
  205. ret = regmap_update_bits(priv->regmap, RT4505_REG_ENABLE,
  206. RT4505_ENABLE_MASK, val);
  207. mutex_unlock(&priv->lock);
  208. return ret;
  209. }
  210. static const struct v4l2_flash_ops v4l2_flash_ops = {
  211. .external_strobe_set = rt4505_flash_external_strobe_set,
  212. };
  213. static void rt4505_init_v4l2_config(struct rt4505_priv *priv,
  214. struct v4l2_flash_config *config)
  215. {
  216. struct led_classdev_flash *flash = &priv->flash;
  217. struct led_classdev *lcdev = &flash->led_cdev;
  218. struct led_flash_setting *s;
  219. strscpy(config->dev_name, lcdev->dev->kobj.name,
  220. sizeof(config->dev_name));
  221. s = &config->intensity;
  222. s->min = RT4505_ITORCH_MINUA;
  223. s->step = RT4505_ITORCH_STPUA;
  224. s->max = s->val = s->min + (lcdev->max_brightness - 1) * s->step;
  225. config->flash_faults = LED_FAULT_OVER_VOLTAGE |
  226. LED_FAULT_SHORT_CIRCUIT |
  227. LED_FAULT_LED_OVER_TEMPERATURE |
  228. LED_FAULT_TIMEOUT;
  229. config->has_external_strobe = 1;
  230. }
  231. #else
  232. static const struct v4l2_flash_ops v4l2_flash_ops;
  233. static void rt4505_init_v4l2_config(struct rt4505_priv *priv,
  234. struct v4l2_flash_config *config)
  235. {
  236. }
  237. #endif
  238. static void rt4505_init_flash_properties(struct rt4505_priv *priv,
  239. struct fwnode_handle *child)
  240. {
  241. struct led_classdev_flash *flash = &priv->flash;
  242. struct led_classdev *lcdev = &flash->led_cdev;
  243. struct led_flash_setting *s;
  244. u32 val;
  245. int ret;
  246. ret = fwnode_property_read_u32(child, "led-max-microamp", &val);
  247. if (ret) {
  248. dev_warn(priv->dev, "led-max-microamp DT property missing\n");
  249. val = RT4505_ITORCH_MINUA;
  250. } else
  251. val = clamp_val(val, RT4505_ITORCH_MINUA, RT4505_ITORCH_MAXUA);
  252. lcdev->max_brightness =
  253. (val - RT4505_ITORCH_MINUA) / RT4505_ITORCH_STPUA + 1;
  254. lcdev->brightness_set_blocking = rt4505_torch_brightness_set;
  255. lcdev->brightness_get = rt4505_torch_brightness_get;
  256. lcdev->flags |= LED_DEV_CAP_FLASH;
  257. ret = fwnode_property_read_u32(child, "flash-max-microamp", &val);
  258. if (ret) {
  259. dev_warn(priv->dev, "flash-max-microamp DT property missing\n");
  260. val = RT4505_IFLASH_MINUA;
  261. } else
  262. val = clamp_val(val, RT4505_IFLASH_MINUA, RT4505_IFLASH_MAXUA);
  263. s = &flash->brightness;
  264. s->min = RT4505_IFLASH_MINUA;
  265. s->step = RT4505_IFLASH_STPUA;
  266. s->max = s->val = val;
  267. ret = fwnode_property_read_u32(child, "flash-max-timeout-us", &val);
  268. if (ret) {
  269. dev_warn(priv->dev,
  270. "flash-max-timeout-us DT property missing\n");
  271. val = RT4505_FLASHTO_MINUS;
  272. } else
  273. val = clamp_val(val, RT4505_FLASHTO_MINUS,
  274. RT4505_FLASHTO_MAXUS);
  275. s = &flash->timeout;
  276. s->min = RT4505_FLASHTO_MINUS;
  277. s->step = RT4505_FLASHTO_STPUS;
  278. s->max = s->val = val;
  279. flash->ops = &rt4505_flash_ops;
  280. }
  281. static int rt4505_probe(struct i2c_client *client)
  282. {
  283. struct rt4505_priv *priv;
  284. struct fwnode_handle *child;
  285. struct led_init_data init_data = {};
  286. struct v4l2_flash_config v4l2_config = {};
  287. int ret;
  288. priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
  289. if (!priv)
  290. return -ENOMEM;
  291. priv->dev = &client->dev;
  292. mutex_init(&priv->lock);
  293. priv->regmap = devm_regmap_init_i2c(client, &rt4505_regmap_config);
  294. if (IS_ERR(priv->regmap)) {
  295. dev_err(priv->dev, "Failed to allocate register map\n");
  296. return PTR_ERR(priv->regmap);
  297. }
  298. ret = regmap_write(priv->regmap, RT4505_REG_RESET, RT4505_RESET_MASK);
  299. if (ret) {
  300. dev_err(priv->dev, "Failed to reset registers\n");
  301. return ret;
  302. }
  303. child = fwnode_get_next_available_child_node(client->dev.fwnode, NULL);
  304. if (!child) {
  305. dev_err(priv->dev, "Failed to get child node\n");
  306. return -EINVAL;
  307. }
  308. init_data.fwnode = child;
  309. rt4505_init_flash_properties(priv, child);
  310. ret = devm_led_classdev_flash_register_ext(priv->dev, &priv->flash,
  311. &init_data);
  312. if (ret) {
  313. dev_err(priv->dev, "Failed to register flash\n");
  314. return ret;
  315. }
  316. rt4505_init_v4l2_config(priv, &v4l2_config);
  317. priv->v4l2_flash = v4l2_flash_init(priv->dev, init_data.fwnode,
  318. &priv->flash, &v4l2_flash_ops,
  319. &v4l2_config);
  320. if (IS_ERR(priv->v4l2_flash)) {
  321. dev_err(priv->dev, "Failed to register v4l2 flash\n");
  322. return PTR_ERR(priv->v4l2_flash);
  323. }
  324. i2c_set_clientdata(client, priv);
  325. return 0;
  326. }
  327. static void rt4505_remove(struct i2c_client *client)
  328. {
  329. struct rt4505_priv *priv = i2c_get_clientdata(client);
  330. v4l2_flash_release(priv->v4l2_flash);
  331. }
  332. static void rt4505_shutdown(struct i2c_client *client)
  333. {
  334. struct rt4505_priv *priv = i2c_get_clientdata(client);
  335. /* Reset registers to make sure all off before shutdown */
  336. regmap_write(priv->regmap, RT4505_REG_RESET, RT4505_RESET_MASK);
  337. }
  338. static const struct of_device_id __maybe_unused rt4505_leds_match[] = {
  339. { .compatible = "richtek,rt4505", },
  340. {}
  341. };
  342. MODULE_DEVICE_TABLE(of, rt4505_leds_match);
  343. static struct i2c_driver rt4505_driver = {
  344. .driver = {
  345. .name = "rt4505",
  346. .of_match_table = of_match_ptr(rt4505_leds_match),
  347. },
  348. .probe_new = rt4505_probe,
  349. .remove = rt4505_remove,
  350. .shutdown = rt4505_shutdown,
  351. };
  352. module_i2c_driver(rt4505_driver);
  353. MODULE_AUTHOR("ChiYuan Huang <[email protected]>");
  354. MODULE_LICENSE("GPL v2");