File Index
Files and directories
Sign in
FILE

leds-mlxcpld.c

File preview with syntax highlighting.

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