leds-mlxcpld.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*
  2. * drivers/leds/leds-mlxcpld.c
  3. * Copyright (c) 2016 Mellanox Technologies. All rights reserved.
  4. * Copyright (c) 2016 Vadim Pasternak <vadimp@mellanox.com>
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the names of the copyright holders nor the names of its
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * Alternatively, this software may be distributed under the terms of the
  19. * GNU General Public License ("GPL") version 2 as published by the Free
  20. * Software Foundation.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <linux/device.h>
  35. #include <linux/dmi.h>
  36. #include <linux/hwmon.h>
  37. #include <linux/hwmon-sysfs.h>
  38. #include <linux/io.h>
  39. #include <linux/leds.h>
  40. #include <linux/module.h>
  41. #include <linux/mod_devicetable.h>
  42. #include <linux/platform_device.h>
  43. #include <linux/slab.h>
  44. #define MLXPLAT_CPLD_LPC_REG_BASE_ADRR 0x2500 /* LPC bus access */
  45. /* Color codes for LEDs */
  46. #define MLXCPLD_LED_OFFSET_HALF 0x01 /* Offset from solid: 3Hz blink */
  47. #define MLXCPLD_LED_OFFSET_FULL 0x02 /* Offset from solid: 6Hz blink */
  48. #define MLXCPLD_LED_IS_OFF 0x00 /* Off */
  49. #define MLXCPLD_LED_RED_STATIC_ON 0x05 /* Solid red */
  50. #define MLXCPLD_LED_RED_BLINK_HALF (MLXCPLD_LED_RED_STATIC_ON + \
  51. MLXCPLD_LED_OFFSET_HALF)
  52. #define MLXCPLD_LED_RED_BLINK_FULL (MLXCPLD_LED_RED_STATIC_ON + \
  53. MLXCPLD_LED_OFFSET_FULL)
  54. #define MLXCPLD_LED_GREEN_STATIC_ON 0x0D /* Solid green */
  55. #define MLXCPLD_LED_GREEN_BLINK_HALF (MLXCPLD_LED_GREEN_STATIC_ON + \
  56. MLXCPLD_LED_OFFSET_HALF)
  57. #define MLXCPLD_LED_GREEN_BLINK_FULL (MLXCPLD_LED_GREEN_STATIC_ON + \
  58. MLXCPLD_LED_OFFSET_FULL)
  59. #define MLXCPLD_LED_BLINK_3HZ 167 /* ~167 msec off/on */
  60. #define MLXCPLD_LED_BLINK_6HZ 83 /* ~83 msec off/on */
  61. /**
  62. * struct mlxcpld_param - LED access parameters:
  63. * @offset: offset for LED access in CPLD device
  64. * @mask: mask for LED access in CPLD device
  65. * @base_color: base color code for LED
  66. **/
  67. struct mlxcpld_param {
  68. u8 offset;
  69. u8 mask;
  70. u8 base_color;
  71. };
  72. /**
  73. * struct mlxcpld_led_priv - LED private data:
  74. * @cdev: LED class device instance
  75. * @param: LED CPLD access parameters
  76. **/
  77. struct mlxcpld_led_priv {
  78. struct led_classdev cdev;
  79. struct mlxcpld_param param;
  80. };
  81. #define cdev_to_priv(c) container_of(c, struct mlxcpld_led_priv, cdev)
  82. /**
  83. * struct mlxcpld_led_profile - system LED profile (defined per system class):
  84. * @offset: offset for LED access in CPLD device
  85. * @mask: mask for LED access in CPLD device
  86. * @base_color: base color code
  87. * @brightness: default brightness setting (on/off)
  88. * @name: LED name
  89. **/
  90. struct mlxcpld_led_profile {
  91. u8 offset;
  92. u8 mask;
  93. u8 base_color;
  94. enum led_brightness brightness;
  95. const char *name;
  96. };
  97. /**
  98. * struct mlxcpld_led_pdata - system LED private data
  99. * @pdev: platform device pointer
  100. * @pled: LED class device instance
  101. * @profile: system configuration profile
  102. * @num_led_instances: number of LED instances
  103. * @lock: device access lock
  104. **/
  105. struct mlxcpld_led_pdata {
  106. struct platform_device *pdev;
  107. struct mlxcpld_led_priv *pled;
  108. struct mlxcpld_led_profile *profile;
  109. int num_led_instances;
  110. spinlock_t lock;
  111. };
  112. static struct mlxcpld_led_pdata *mlxcpld_led;
  113. /* Default profile fit the next Mellanox systems:
  114. * "msx6710", "msx6720", "msb7700", "msn2700", "msx1410",
  115. * "msn2410", "msb7800", "msn2740"
  116. */
  117. static struct mlxcpld_led_profile mlxcpld_led_default_profile[] = {
  118. {
  119. 0x21, 0xf0, MLXCPLD_LED_GREEN_STATIC_ON, 1,
  120. "mlxcpld:fan1:green",
  121. },
  122. {
  123. 0x21, 0xf0, MLXCPLD_LED_RED_STATIC_ON, LED_OFF,
  124. "mlxcpld:fan1:red",
  125. },
  126. {
  127. 0x21, 0x0f, MLXCPLD_LED_GREEN_STATIC_ON, 1,
  128. "mlxcpld:fan2:green",
  129. },
  130. {
  131. 0x21, 0x0f, MLXCPLD_LED_RED_STATIC_ON, LED_OFF,
  132. "mlxcpld:fan2:red",
  133. },
  134. {
  135. 0x22, 0xf0, MLXCPLD_LED_GREEN_STATIC_ON, 1,
  136. "mlxcpld:fan3:green",
  137. },
  138. {
  139. 0x22, 0xf0, MLXCPLD_LED_RED_STATIC_ON, LED_OFF,
  140. "mlxcpld:fan3:red",
  141. },
  142. {
  143. 0x22, 0x0f, MLXCPLD_LED_GREEN_STATIC_ON, 1,
  144. "mlxcpld:fan4:green",
  145. },
  146. {
  147. 0x22, 0x0f, MLXCPLD_LED_RED_STATIC_ON, LED_OFF,
  148. "mlxcpld:fan4:red",
  149. },
  150. {
  151. 0x20, 0x0f, MLXCPLD_LED_GREEN_STATIC_ON, 1,
  152. "mlxcpld:psu:green",
  153. },
  154. {
  155. 0x20, 0x0f, MLXCPLD_LED_RED_STATIC_ON, LED_OFF,
  156. "mlxcpld:psu:red",
  157. },
  158. {
  159. 0x20, 0xf0, MLXCPLD_LED_GREEN_STATIC_ON, 1,
  160. "mlxcpld:status:green",
  161. },
  162. {
  163. 0x20, 0xf0, MLXCPLD_LED_RED_STATIC_ON, LED_OFF,
  164. "mlxcpld:status:red",
  165. },
  166. };
  167. /* Profile fit the Mellanox systems based on "msn2100" */
  168. static struct mlxcpld_led_profile mlxcpld_led_msn2100_profile[] = {
  169. {
  170. 0x21, 0xf0, MLXCPLD_LED_GREEN_STATIC_ON, 1,
  171. "mlxcpld:fan:green",
  172. },
  173. {
  174. 0x21, 0xf0, MLXCPLD_LED_RED_STATIC_ON, LED_OFF,
  175. "mlxcpld:fan:red",
  176. },
  177. {
  178. 0x23, 0xf0, MLXCPLD_LED_GREEN_STATIC_ON, 1,
  179. "mlxcpld:psu1:green",
  180. },
  181. {
  182. 0x23, 0xf0, MLXCPLD_LED_RED_STATIC_ON, LED_OFF,
  183. "mlxcpld:psu1:red",
  184. },
  185. {
  186. 0x23, 0x0f, MLXCPLD_LED_GREEN_STATIC_ON, 1,
  187. "mlxcpld:psu2:green",
  188. },
  189. {
  190. 0x23, 0x0f, MLXCPLD_LED_RED_STATIC_ON, LED_OFF,
  191. "mlxcpld:psu2:red",
  192. },
  193. {
  194. 0x20, 0xf0, MLXCPLD_LED_GREEN_STATIC_ON, 1,
  195. "mlxcpld:status:green",
  196. },
  197. {
  198. 0x20, 0xf0, MLXCPLD_LED_RED_STATIC_ON, LED_OFF,
  199. "mlxcpld:status:red",
  200. },
  201. {
  202. 0x24, 0xf0, MLXCPLD_LED_GREEN_STATIC_ON, LED_OFF,
  203. "mlxcpld:uid:blue",
  204. },
  205. };
  206. enum mlxcpld_led_platform_types {
  207. MLXCPLD_LED_PLATFORM_DEFAULT,
  208. MLXCPLD_LED_PLATFORM_MSN2100,
  209. };
  210. static const char *mlx_product_names[] = {
  211. "DEFAULT",
  212. "MSN2100",
  213. };
  214. static enum
  215. mlxcpld_led_platform_types mlxcpld_led_platform_check_sys_type(void)
  216. {
  217. const char *mlx_product_name;
  218. int i;
  219. mlx_product_name = dmi_get_system_info(DMI_PRODUCT_NAME);
  220. if (!mlx_product_name)
  221. return MLXCPLD_LED_PLATFORM_DEFAULT;
  222. for (i = 1; i < ARRAY_SIZE(mlx_product_names); i++) {
  223. if (strstr(mlx_product_name, mlx_product_names[i]))
  224. return i;
  225. }
  226. return MLXCPLD_LED_PLATFORM_DEFAULT;
  227. }
  228. static void mlxcpld_led_bus_access_func(u16 base, u8 offset, u8 rw_flag,
  229. u8 *data)
  230. {
  231. u32 addr = base + offset;
  232. if (rw_flag == 0)
  233. outb(*data, addr);
  234. else
  235. *data = inb(addr);
  236. }
  237. static void mlxcpld_led_store_hw(u8 mask, u8 off, u8 vset)
  238. {
  239. u8 nib, val;
  240. /*
  241. * Each LED is controlled through low or high nibble of the relevant
  242. * CPLD register. Register offset is specified by off parameter.
  243. * Parameter vset provides color code: 0x0 for off, 0x5 for solid red,
  244. * 0x6 for 3Hz blink red, 0xd for solid green, 0xe for 3Hz blink
  245. * green.
  246. * Parameter mask specifies which nibble is used for specific LED: mask
  247. * 0xf0 - lower nibble is to be used (bits from 0 to 3), mask 0x0f -
  248. * higher nibble (bits from 4 to 7).
  249. */
  250. spin_lock(&mlxcpld_led->lock);
  251. mlxcpld_led_bus_access_func(MLXPLAT_CPLD_LPC_REG_BASE_ADRR, off, 1,
  252. &val);
  253. nib = (mask == 0xf0) ? vset : (vset << 4);
  254. val = (val & mask) | nib;
  255. mlxcpld_led_bus_access_func(MLXPLAT_CPLD_LPC_REG_BASE_ADRR, off, 0,
  256. &val);
  257. spin_unlock(&mlxcpld_led->lock);
  258. }
  259. static void mlxcpld_led_brightness_set(struct led_classdev *led,
  260. enum led_brightness value)
  261. {
  262. struct mlxcpld_led_priv *pled = cdev_to_priv(led);
  263. if (value) {
  264. mlxcpld_led_store_hw(pled->param.mask, pled->param.offset,
  265. pled->param.base_color);
  266. return;
  267. }
  268. mlxcpld_led_store_hw(pled->param.mask, pled->param.offset,
  269. MLXCPLD_LED_IS_OFF);
  270. }
  271. static int mlxcpld_led_blink_set(struct led_classdev *led,
  272. unsigned long *delay_on,
  273. unsigned long *delay_off)
  274. {
  275. struct mlxcpld_led_priv *pled = cdev_to_priv(led);
  276. /*
  277. * HW supports two types of blinking: full (6Hz) and half (3Hz).
  278. * For delay on/off zero default setting 3Hz is used.
  279. */
  280. if (!(*delay_on == 0 && *delay_off == 0) &&
  281. !(*delay_on == MLXCPLD_LED_BLINK_3HZ &&
  282. *delay_off == MLXCPLD_LED_BLINK_3HZ) &&
  283. !(*delay_on == MLXCPLD_LED_BLINK_6HZ &&
  284. *delay_off == MLXCPLD_LED_BLINK_6HZ))
  285. return -EINVAL;
  286. if (*delay_on == MLXCPLD_LED_BLINK_6HZ)
  287. mlxcpld_led_store_hw(pled->param.mask, pled->param.offset,
  288. pled->param.base_color +
  289. MLXCPLD_LED_OFFSET_FULL);
  290. else
  291. mlxcpld_led_store_hw(pled->param.mask, pled->param.offset,
  292. pled->param.base_color +
  293. MLXCPLD_LED_OFFSET_HALF);
  294. return 0;
  295. }
  296. static int mlxcpld_led_config(struct device *dev,
  297. struct mlxcpld_led_pdata *cpld)
  298. {
  299. int i;
  300. int err;
  301. cpld->pled = devm_kcalloc(dev,
  302. cpld->num_led_instances,
  303. sizeof(struct mlxcpld_led_priv),
  304. GFP_KERNEL);
  305. if (!cpld->pled)
  306. return -ENOMEM;
  307. for (i = 0; i < cpld->num_led_instances; i++) {
  308. cpld->pled[i].cdev.name = cpld->profile[i].name;
  309. cpld->pled[i].cdev.brightness = cpld->profile[i].brightness;
  310. cpld->pled[i].cdev.max_brightness = 1;
  311. cpld->pled[i].cdev.brightness_set = mlxcpld_led_brightness_set;
  312. cpld->pled[i].cdev.blink_set = mlxcpld_led_blink_set;
  313. cpld->pled[i].cdev.flags = LED_CORE_SUSPENDRESUME;
  314. err = devm_led_classdev_register(dev, &cpld->pled[i].cdev);
  315. if (err)
  316. return err;
  317. cpld->pled[i].param.offset = mlxcpld_led->profile[i].offset;
  318. cpld->pled[i].param.mask = mlxcpld_led->profile[i].mask;
  319. cpld->pled[i].param.base_color =
  320. mlxcpld_led->profile[i].base_color;
  321. if (mlxcpld_led->profile[i].brightness)
  322. mlxcpld_led_brightness_set(&cpld->pled[i].cdev,
  323. mlxcpld_led->profile[i].brightness);
  324. }
  325. return 0;
  326. }
  327. static int __init mlxcpld_led_probe(struct platform_device *pdev)
  328. {
  329. enum mlxcpld_led_platform_types mlxcpld_led_plat =
  330. mlxcpld_led_platform_check_sys_type();
  331. mlxcpld_led = devm_kzalloc(&pdev->dev, sizeof(*mlxcpld_led),
  332. GFP_KERNEL);
  333. if (!mlxcpld_led)
  334. return -ENOMEM;
  335. mlxcpld_led->pdev = pdev;
  336. switch (mlxcpld_led_plat) {
  337. case MLXCPLD_LED_PLATFORM_MSN2100:
  338. mlxcpld_led->profile = mlxcpld_led_msn2100_profile;
  339. mlxcpld_led->num_led_instances =
  340. ARRAY_SIZE(mlxcpld_led_msn2100_profile);
  341. break;
  342. default:
  343. mlxcpld_led->profile = mlxcpld_led_default_profile;
  344. mlxcpld_led->num_led_instances =
  345. ARRAY_SIZE(mlxcpld_led_default_profile);
  346. break;
  347. }
  348. spin_lock_init(&mlxcpld_led->lock);
  349. return mlxcpld_led_config(&pdev->dev, mlxcpld_led);
  350. }
  351. static struct platform_driver mlxcpld_led_driver = {
  352. .driver = {
  353. .name = KBUILD_MODNAME,
  354. },
  355. };
  356. static int __init mlxcpld_led_init(void)
  357. {
  358. struct platform_device *pdev;
  359. int err;
  360. if (!dmi_match(DMI_CHASSIS_VENDOR, "Mellanox Technologies Ltd."))
  361. return -ENODEV;
  362. pdev = platform_device_register_simple(KBUILD_MODNAME, -1, NULL, 0);
  363. if (IS_ERR(pdev)) {
  364. pr_err("Device allocation failed\n");
  365. return PTR_ERR(pdev);
  366. }
  367. err = platform_driver_probe(&mlxcpld_led_driver, mlxcpld_led_probe);
  368. if (err) {
  369. pr_err("Probe platform driver failed\n");
  370. platform_device_unregister(pdev);
  371. }
  372. return err;
  373. }
  374. static void __exit mlxcpld_led_exit(void)
  375. {
  376. platform_device_unregister(mlxcpld_led->pdev);
  377. platform_driver_unregister(&mlxcpld_led_driver);
  378. }
  379. module_init(mlxcpld_led_init);
  380. module_exit(mlxcpld_led_exit);
  381. MODULE_AUTHOR("Vadim Pasternak <vadimp@mellanox.com>");
  382. MODULE_DESCRIPTION("Mellanox board LED driver");
  383. MODULE_LICENSE("Dual BSD/GPL");
  384. MODULE_ALIAS("platform:leds_mlxcpld");