leds-mlxcpld.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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/acpi.h>
  35. #include <linux/device.h>
  36. #include <linux/dmi.h>
  37. #include <linux/hwmon.h>
  38. #include <linux/hwmon-sysfs.h>
  39. #include <linux/io.h>
  40. #include <linux/leds.h>
  41. #include <linux/module.h>
  42. #include <linux/mod_devicetable.h>
  43. #include <linux/platform_device.h>
  44. #include <linux/slab.h>
  45. #define MLXPLAT_CPLD_LPC_REG_BASE_ADRR 0x2500 /* LPC bus access */
  46. /* Color codes for LEDs */
  47. #define MLXCPLD_LED_OFFSET_HALF 0x01 /* Offset from solid: 3Hz blink */
  48. #define MLXCPLD_LED_OFFSET_FULL 0x02 /* Offset from solid: 6Hz blink */
  49. #define MLXCPLD_LED_IS_OFF 0x00 /* Off */
  50. #define MLXCPLD_LED_RED_STATIC_ON 0x05 /* Solid red */
  51. #define MLXCPLD_LED_RED_BLINK_HALF (MLXCPLD_LED_RED_STATIC_ON + \
  52. MLXCPLD_LED_OFFSET_HALF)
  53. #define MLXCPLD_LED_RED_BLINK_FULL (MLXCPLD_LED_RED_STATIC_ON + \
  54. MLXCPLD_LED_OFFSET_FULL)
  55. #define MLXCPLD_LED_GREEN_STATIC_ON 0x0D /* Solid green */
  56. #define MLXCPLD_LED_GREEN_BLINK_HALF (MLXCPLD_LED_GREEN_STATIC_ON + \
  57. MLXCPLD_LED_OFFSET_HALF)
  58. #define MLXCPLD_LED_GREEN_BLINK_FULL (MLXCPLD_LED_GREEN_STATIC_ON + \
  59. MLXCPLD_LED_OFFSET_FULL)
  60. #define MLXCPLD_LED_BLINK_3HZ 167 /* ~167 msec off/on */
  61. #define MLXCPLD_LED_BLINK_6HZ 83 /* ~83 msec off/on */
  62. /**
  63. * mlxcpld_param - LED access parameters:
  64. * @offset - offset for LED access in CPLD device
  65. * @mask - mask for LED access in CPLD device
  66. * @base_color - base color code for LED
  67. **/
  68. struct mlxcpld_param {
  69. u8 offset;
  70. u8 mask;
  71. u8 base_color;
  72. };
  73. /**
  74. * mlxcpld_led_priv - LED private data:
  75. * @cled - LED class device instance
  76. * @param - LED CPLD access parameters
  77. **/
  78. struct mlxcpld_led_priv {
  79. struct led_classdev cdev;
  80. struct mlxcpld_param param;
  81. };
  82. #define cdev_to_priv(c) container_of(c, struct mlxcpld_led_priv, cdev)
  83. /**
  84. * mlxcpld_led_profile - system LED profile (defined per system class):
  85. * @offset - offset for LED access in CPLD device
  86. * @mask - mask for LED access in CPLD device
  87. * @base_color - base color code
  88. * @brightness - default brightness setting (on/off)
  89. * @name - LED name
  90. **/
  91. struct mlxcpld_led_profile {
  92. u8 offset;
  93. u8 mask;
  94. u8 base_color;
  95. enum led_brightness brightness;
  96. const char *name;
  97. };
  98. /**
  99. * mlxcpld_led_pdata - system LED private data
  100. * @pdev - platform device pointer
  101. * @pled - LED class device instance
  102. * @profile - system configuration profile
  103. * @num_led_instances - number of LED instances
  104. * @lock - device access lock
  105. **/
  106. struct mlxcpld_led_pdata {
  107. struct platform_device *pdev;
  108. struct mlxcpld_led_priv *pled;
  109. struct mlxcpld_led_profile *profile;
  110. int num_led_instances;
  111. spinlock_t lock;
  112. };
  113. static struct mlxcpld_led_pdata *mlxcpld_led;
  114. /* Default profile fit the next Mellanox systems:
  115. * "msx6710", "msx6720", "msb7700", "msn2700", "msx1410",
  116. * "msn2410", "msb7800", "msn2740"
  117. */
  118. static struct mlxcpld_led_profile mlxcpld_led_default_profile[] = {
  119. {
  120. 0x21, 0xf0, MLXCPLD_LED_GREEN_STATIC_ON, 1,
  121. "mlxcpld:fan1:green",
  122. },
  123. {
  124. 0x21, 0xf0, MLXCPLD_LED_RED_STATIC_ON, LED_OFF,
  125. "mlxcpld:fan1:red",
  126. },
  127. {
  128. 0x21, 0x0f, MLXCPLD_LED_GREEN_STATIC_ON, 1,
  129. "mlxcpld:fan2:green",
  130. },
  131. {
  132. 0x21, 0x0f, MLXCPLD_LED_RED_STATIC_ON, LED_OFF,
  133. "mlxcpld:fan2:red",
  134. },
  135. {
  136. 0x22, 0xf0, MLXCPLD_LED_GREEN_STATIC_ON, 1,
  137. "mlxcpld:fan3:green",
  138. },
  139. {
  140. 0x22, 0xf0, MLXCPLD_LED_RED_STATIC_ON, LED_OFF,
  141. "mlxcpld:fan3:red",
  142. },
  143. {
  144. 0x22, 0x0f, MLXCPLD_LED_GREEN_STATIC_ON, 1,
  145. "mlxcpld:fan4:green",
  146. },
  147. {
  148. 0x22, 0x0f, MLXCPLD_LED_RED_STATIC_ON, LED_OFF,
  149. "mlxcpld:fan4:red",
  150. },
  151. {
  152. 0x20, 0x0f, MLXCPLD_LED_GREEN_STATIC_ON, 1,
  153. "mlxcpld:psu:green",
  154. },
  155. {
  156. 0x20, 0x0f, MLXCPLD_LED_RED_STATIC_ON, LED_OFF,
  157. "mlxcpld:psu:red",
  158. },
  159. {
  160. 0x20, 0xf0, MLXCPLD_LED_GREEN_STATIC_ON, 1,
  161. "mlxcpld:status:green",
  162. },
  163. {
  164. 0x20, 0xf0, MLXCPLD_LED_RED_STATIC_ON, LED_OFF,
  165. "mlxcpld:status:red",
  166. },
  167. };
  168. /* Profile fit the Mellanox systems based on "msn2100" */
  169. static struct mlxcpld_led_profile mlxcpld_led_msn2100_profile[] = {
  170. {
  171. 0x21, 0xf0, MLXCPLD_LED_GREEN_STATIC_ON, 1,
  172. "mlxcpld:fan:green",
  173. },
  174. {
  175. 0x21, 0xf0, MLXCPLD_LED_RED_STATIC_ON, LED_OFF,
  176. "mlxcpld:fan:red",
  177. },
  178. {
  179. 0x23, 0xf0, MLXCPLD_LED_GREEN_STATIC_ON, 1,
  180. "mlxcpld:psu1:green",
  181. },
  182. {
  183. 0x23, 0xf0, MLXCPLD_LED_RED_STATIC_ON, LED_OFF,
  184. "mlxcpld:psu1:red",
  185. },
  186. {
  187. 0x23, 0x0f, MLXCPLD_LED_GREEN_STATIC_ON, 1,
  188. "mlxcpld:psu2:green",
  189. },
  190. {
  191. 0x23, 0x0f, MLXCPLD_LED_RED_STATIC_ON, LED_OFF,
  192. "mlxcpld:psu2:red",
  193. },
  194. {
  195. 0x20, 0xf0, MLXCPLD_LED_GREEN_STATIC_ON, 1,
  196. "mlxcpld:status:green",
  197. },
  198. {
  199. 0x20, 0xf0, MLXCPLD_LED_RED_STATIC_ON, LED_OFF,
  200. "mlxcpld:status:red",
  201. },
  202. {
  203. 0x24, 0xf0, MLXCPLD_LED_GREEN_STATIC_ON, LED_OFF,
  204. "mlxcpld:uid:blue",
  205. },
  206. };
  207. enum mlxcpld_led_platform_types {
  208. MLXCPLD_LED_PLATFORM_DEFAULT,
  209. MLXCPLD_LED_PLATFORM_MSN2100,
  210. };
  211. static const char *mlx_product_names[] = {
  212. "DEFAULT",
  213. "MSN2100",
  214. };
  215. static enum
  216. mlxcpld_led_platform_types mlxcpld_led_platform_check_sys_type(void)
  217. {
  218. const char *mlx_product_name;
  219. int i;
  220. mlx_product_name = dmi_get_system_info(DMI_PRODUCT_NAME);
  221. if (!mlx_product_name)
  222. return MLXCPLD_LED_PLATFORM_DEFAULT;
  223. for (i = 1; i < ARRAY_SIZE(mlx_product_names); i++) {
  224. if (strstr(mlx_product_name, mlx_product_names[i]))
  225. return i;
  226. }
  227. return MLXCPLD_LED_PLATFORM_DEFAULT;
  228. }
  229. static void mlxcpld_led_bus_access_func(u16 base, u8 offset, u8 rw_flag,
  230. u8 *data)
  231. {
  232. u32 addr = base + offset;
  233. if (rw_flag == 0)
  234. outb(*data, addr);
  235. else
  236. *data = inb(addr);
  237. }
  238. static void mlxcpld_led_store_hw(u8 mask, u8 off, u8 vset)
  239. {
  240. u8 nib, val;
  241. /*
  242. * Each LED is controlled through low or high nibble of the relevant
  243. * CPLD register. Register offset is specified by off parameter.
  244. * Parameter vset provides color code: 0x0 for off, 0x5 for solid red,
  245. * 0x6 for 3Hz blink red, 0xd for solid green, 0xe for 3Hz blink
  246. * green.
  247. * Parameter mask specifies which nibble is used for specific LED: mask
  248. * 0xf0 - lower nibble is to be used (bits from 0 to 3), mask 0x0f -
  249. * higher nibble (bits from 4 to 7).
  250. */
  251. spin_lock(&mlxcpld_led->lock);
  252. mlxcpld_led_bus_access_func(MLXPLAT_CPLD_LPC_REG_BASE_ADRR, off, 1,
  253. &val);
  254. nib = (mask == 0xf0) ? vset : (vset << 4);
  255. val = (val & mask) | nib;
  256. mlxcpld_led_bus_access_func(MLXPLAT_CPLD_LPC_REG_BASE_ADRR, off, 0,
  257. &val);
  258. spin_unlock(&mlxcpld_led->lock);
  259. }
  260. static void mlxcpld_led_brightness_set(struct led_classdev *led,
  261. enum led_brightness value)
  262. {
  263. struct mlxcpld_led_priv *pled = cdev_to_priv(led);
  264. if (value) {
  265. mlxcpld_led_store_hw(pled->param.mask, pled->param.offset,
  266. pled->param.base_color);
  267. return;
  268. }
  269. mlxcpld_led_store_hw(pled->param.mask, pled->param.offset,
  270. MLXCPLD_LED_IS_OFF);
  271. }
  272. static int mlxcpld_led_blink_set(struct led_classdev *led,
  273. unsigned long *delay_on,
  274. unsigned long *delay_off)
  275. {
  276. struct mlxcpld_led_priv *pled = cdev_to_priv(led);
  277. /*
  278. * HW supports two types of blinking: full (6Hz) and half (3Hz).
  279. * For delay on/off zero default setting 3Hz is used.
  280. */
  281. if (!(*delay_on == 0 && *delay_off == 0) &&
  282. !(*delay_on == MLXCPLD_LED_BLINK_3HZ &&
  283. *delay_off == MLXCPLD_LED_BLINK_3HZ) &&
  284. !(*delay_on == MLXCPLD_LED_BLINK_6HZ &&
  285. *delay_off == MLXCPLD_LED_BLINK_6HZ))
  286. return -EINVAL;
  287. if (*delay_on == MLXCPLD_LED_BLINK_6HZ)
  288. mlxcpld_led_store_hw(pled->param.mask, pled->param.offset,
  289. pled->param.base_color +
  290. MLXCPLD_LED_OFFSET_FULL);
  291. else
  292. mlxcpld_led_store_hw(pled->param.mask, pled->param.offset,
  293. pled->param.base_color +
  294. MLXCPLD_LED_OFFSET_HALF);
  295. return 0;
  296. }
  297. static int mlxcpld_led_config(struct device *dev,
  298. struct mlxcpld_led_pdata *cpld)
  299. {
  300. int i;
  301. int err;
  302. cpld->pled = devm_kcalloc(dev,
  303. cpld->num_led_instances,
  304. sizeof(struct mlxcpld_led_priv),
  305. GFP_KERNEL);
  306. if (!cpld->pled)
  307. return -ENOMEM;
  308. for (i = 0; i < cpld->num_led_instances; i++) {
  309. cpld->pled[i].cdev.name = cpld->profile[i].name;
  310. cpld->pled[i].cdev.brightness = cpld->profile[i].brightness;
  311. cpld->pled[i].cdev.max_brightness = 1;
  312. cpld->pled[i].cdev.brightness_set = mlxcpld_led_brightness_set;
  313. cpld->pled[i].cdev.blink_set = mlxcpld_led_blink_set;
  314. cpld->pled[i].cdev.flags = LED_CORE_SUSPENDRESUME;
  315. err = devm_led_classdev_register(dev, &cpld->pled[i].cdev);
  316. if (err)
  317. return err;
  318. cpld->pled[i].param.offset = mlxcpld_led->profile[i].offset;
  319. cpld->pled[i].param.mask = mlxcpld_led->profile[i].mask;
  320. cpld->pled[i].param.base_color =
  321. mlxcpld_led->profile[i].base_color;
  322. if (mlxcpld_led->profile[i].brightness)
  323. mlxcpld_led_brightness_set(&cpld->pled[i].cdev,
  324. mlxcpld_led->profile[i].brightness);
  325. }
  326. return 0;
  327. }
  328. static int __init mlxcpld_led_probe(struct platform_device *pdev)
  329. {
  330. enum mlxcpld_led_platform_types mlxcpld_led_plat =
  331. mlxcpld_led_platform_check_sys_type();
  332. mlxcpld_led = devm_kzalloc(&pdev->dev, sizeof(*mlxcpld_led),
  333. GFP_KERNEL);
  334. if (!mlxcpld_led)
  335. return -ENOMEM;
  336. mlxcpld_led->pdev = pdev;
  337. switch (mlxcpld_led_plat) {
  338. case MLXCPLD_LED_PLATFORM_MSN2100:
  339. mlxcpld_led->profile = mlxcpld_led_msn2100_profile;
  340. mlxcpld_led->num_led_instances =
  341. ARRAY_SIZE(mlxcpld_led_msn2100_profile);
  342. break;
  343. default:
  344. mlxcpld_led->profile = mlxcpld_led_default_profile;
  345. mlxcpld_led->num_led_instances =
  346. ARRAY_SIZE(mlxcpld_led_default_profile);
  347. break;
  348. }
  349. spin_lock_init(&mlxcpld_led->lock);
  350. return mlxcpld_led_config(&pdev->dev, mlxcpld_led);
  351. }
  352. static struct platform_driver mlxcpld_led_driver = {
  353. .driver = {
  354. .name = KBUILD_MODNAME,
  355. },
  356. };
  357. static int __init mlxcpld_led_init(void)
  358. {
  359. struct platform_device *pdev;
  360. int err;
  361. if (!dmi_match(DMI_CHASSIS_VENDOR, "Mellanox Technologies Ltd."))
  362. return -ENODEV;
  363. pdev = platform_device_register_simple(KBUILD_MODNAME, -1, NULL, 0);
  364. if (IS_ERR(pdev)) {
  365. pr_err("Device allocation failed\n");
  366. return PTR_ERR(pdev);
  367. }
  368. err = platform_driver_probe(&mlxcpld_led_driver, mlxcpld_led_probe);
  369. if (err) {
  370. pr_err("Probe platform driver failed\n");
  371. platform_device_unregister(pdev);
  372. }
  373. return err;
  374. }
  375. static void __exit mlxcpld_led_exit(void)
  376. {
  377. platform_device_unregister(mlxcpld_led->pdev);
  378. platform_driver_unregister(&mlxcpld_led_driver);
  379. }
  380. module_init(mlxcpld_led_init);
  381. module_exit(mlxcpld_led_exit);
  382. MODULE_AUTHOR("Vadim Pasternak <vadimp@mellanox.com>");
  383. MODULE_DESCRIPTION("Mellanox board LED driver");
  384. MODULE_LICENSE("Dual BSD/GPL");
  385. MODULE_ALIAS("platform:leds_mlxcpld");