cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

leds-lm3533.c (17926B)


      1// SPDX-License-Identifier: GPL-2.0-or-later
      2/*
      3 * leds-lm3533.c -- LM3533 LED driver
      4 *
      5 * Copyright (C) 2011-2012 Texas Instruments
      6 *
      7 * Author: Johan Hovold <jhovold@gmail.com>
      8 */
      9
     10#include <linux/module.h>
     11#include <linux/leds.h>
     12#include <linux/mfd/core.h>
     13#include <linux/mutex.h>
     14#include <linux/platform_device.h>
     15#include <linux/slab.h>
     16
     17#include <linux/mfd/lm3533.h>
     18
     19
     20#define LM3533_LVCTRLBANK_MIN		2
     21#define LM3533_LVCTRLBANK_MAX		5
     22#define LM3533_LVCTRLBANK_COUNT		4
     23#define LM3533_RISEFALLTIME_MAX		7
     24#define LM3533_ALS_CHANNEL_LV_MIN	1
     25#define LM3533_ALS_CHANNEL_LV_MAX	2
     26
     27#define LM3533_REG_CTRLBANK_BCONF_BASE		0x1b
     28#define LM3533_REG_PATTERN_ENABLE		0x28
     29#define LM3533_REG_PATTERN_LOW_TIME_BASE	0x71
     30#define LM3533_REG_PATTERN_HIGH_TIME_BASE	0x72
     31#define LM3533_REG_PATTERN_RISETIME_BASE	0x74
     32#define LM3533_REG_PATTERN_FALLTIME_BASE	0x75
     33
     34#define LM3533_REG_PATTERN_STEP			0x10
     35
     36#define LM3533_REG_CTRLBANK_BCONF_MAPPING_MASK		0x04
     37#define LM3533_REG_CTRLBANK_BCONF_ALS_EN_MASK		0x02
     38#define LM3533_REG_CTRLBANK_BCONF_ALS_CHANNEL_MASK	0x01
     39
     40#define LM3533_LED_FLAG_PATTERN_ENABLE		1
     41
     42
     43struct lm3533_led {
     44	struct lm3533 *lm3533;
     45	struct lm3533_ctrlbank cb;
     46	struct led_classdev cdev;
     47	int id;
     48
     49	struct mutex mutex;
     50	unsigned long flags;
     51};
     52
     53
     54static inline struct lm3533_led *to_lm3533_led(struct led_classdev *cdev)
     55{
     56	return container_of(cdev, struct lm3533_led, cdev);
     57}
     58
     59static inline int lm3533_led_get_ctrlbank_id(struct lm3533_led *led)
     60{
     61	return led->id + 2;
     62}
     63
     64static inline u8 lm3533_led_get_lv_reg(struct lm3533_led *led, u8 base)
     65{
     66	return base + led->id;
     67}
     68
     69static inline u8 lm3533_led_get_pattern(struct lm3533_led *led)
     70{
     71	return led->id;
     72}
     73
     74static inline u8 lm3533_led_get_pattern_reg(struct lm3533_led *led,
     75								u8 base)
     76{
     77	return base + lm3533_led_get_pattern(led) * LM3533_REG_PATTERN_STEP;
     78}
     79
     80static int lm3533_led_pattern_enable(struct lm3533_led *led, int enable)
     81{
     82	u8 mask;
     83	u8 val;
     84	int pattern;
     85	int state;
     86	int ret = 0;
     87
     88	dev_dbg(led->cdev.dev, "%s - %d\n", __func__, enable);
     89
     90	mutex_lock(&led->mutex);
     91
     92	state = test_bit(LM3533_LED_FLAG_PATTERN_ENABLE, &led->flags);
     93	if ((enable && state) || (!enable && !state))
     94		goto out;
     95
     96	pattern = lm3533_led_get_pattern(led);
     97	mask = 1 << (2 * pattern);
     98
     99	if (enable)
    100		val = mask;
    101	else
    102		val = 0;
    103
    104	ret = lm3533_update(led->lm3533, LM3533_REG_PATTERN_ENABLE, val, mask);
    105	if (ret) {
    106		dev_err(led->cdev.dev, "failed to enable pattern %d (%d)\n",
    107							pattern, enable);
    108		goto out;
    109	}
    110
    111	__change_bit(LM3533_LED_FLAG_PATTERN_ENABLE, &led->flags);
    112out:
    113	mutex_unlock(&led->mutex);
    114
    115	return ret;
    116}
    117
    118static int lm3533_led_set(struct led_classdev *cdev,
    119						enum led_brightness value)
    120{
    121	struct lm3533_led *led = to_lm3533_led(cdev);
    122
    123	dev_dbg(led->cdev.dev, "%s - %d\n", __func__, value);
    124
    125	if (value == 0)
    126		lm3533_led_pattern_enable(led, 0);	/* disable blink */
    127
    128	return lm3533_ctrlbank_set_brightness(&led->cb, value);
    129}
    130
    131static enum led_brightness lm3533_led_get(struct led_classdev *cdev)
    132{
    133	struct lm3533_led *led = to_lm3533_led(cdev);
    134	u8 val;
    135	int ret;
    136
    137	ret = lm3533_ctrlbank_get_brightness(&led->cb, &val);
    138	if (ret)
    139		return ret;
    140
    141	dev_dbg(led->cdev.dev, "%s - %u\n", __func__, val);
    142
    143	return val;
    144}
    145
    146/* Pattern generator defines (delays in us). */
    147#define LM3533_LED_DELAY1_VMIN	0x00
    148#define LM3533_LED_DELAY2_VMIN	0x3d
    149#define LM3533_LED_DELAY3_VMIN	0x80
    150
    151#define LM3533_LED_DELAY1_VMAX	(LM3533_LED_DELAY2_VMIN - 1)
    152#define LM3533_LED_DELAY2_VMAX	(LM3533_LED_DELAY3_VMIN - 1)
    153#define LM3533_LED_DELAY3_VMAX	0xff
    154
    155#define LM3533_LED_DELAY1_TMIN	16384U
    156#define LM3533_LED_DELAY2_TMIN	1130496U
    157#define LM3533_LED_DELAY3_TMIN	10305536U
    158
    159#define LM3533_LED_DELAY1_TMAX	999424U
    160#define LM3533_LED_DELAY2_TMAX	9781248U
    161#define LM3533_LED_DELAY3_TMAX	76890112U
    162
    163/* t_step = (t_max - t_min) / (v_max - v_min) */
    164#define LM3533_LED_DELAY1_TSTEP	16384
    165#define LM3533_LED_DELAY2_TSTEP	131072
    166#define LM3533_LED_DELAY3_TSTEP	524288
    167
    168/* Delay limits for hardware accelerated blinking (in ms). */
    169#define LM3533_LED_DELAY_ON_MAX \
    170	((LM3533_LED_DELAY2_TMAX + LM3533_LED_DELAY2_TSTEP / 2) / 1000)
    171#define LM3533_LED_DELAY_OFF_MAX \
    172	((LM3533_LED_DELAY3_TMAX + LM3533_LED_DELAY3_TSTEP / 2) / 1000)
    173
    174/*
    175 * Returns linear map of *t from [t_min,t_max] to [v_min,v_max] with a step
    176 * size of t_step, where
    177 *
    178 *	t_step = (t_max - t_min) / (v_max - v_min)
    179 *
    180 * and updates *t to reflect the mapped value.
    181 */
    182static u8 time_to_val(unsigned *t, unsigned t_min, unsigned t_step,
    183							u8 v_min, u8 v_max)
    184{
    185	unsigned val;
    186
    187	val = (*t + t_step / 2 - t_min) / t_step + v_min;
    188
    189	*t = t_step * (val - v_min) + t_min;
    190
    191	return (u8)val;
    192}
    193
    194/*
    195 * Returns time code corresponding to *delay (in ms) and updates *delay to
    196 * reflect actual hardware delay.
    197 *
    198 * Hardware supports 256 discrete delay times, divided into three groups with
    199 * the following ranges and step-sizes:
    200 *
    201 *	[   16,   999]	[0x00, 0x3e]	step  16 ms
    202 *	[ 1130,  9781]	[0x3d, 0x7f]	step 131 ms
    203 *	[10306, 76890]	[0x80, 0xff]	step 524 ms
    204 *
    205 * Note that delay group 3 is only available for delay_off.
    206 */
    207static u8 lm3533_led_get_hw_delay(unsigned *delay)
    208{
    209	unsigned t;
    210	u8 val;
    211
    212	t = *delay * 1000;
    213
    214	if (t >= (LM3533_LED_DELAY2_TMAX + LM3533_LED_DELAY3_TMIN) / 2) {
    215		t = clamp(t, LM3533_LED_DELAY3_TMIN, LM3533_LED_DELAY3_TMAX);
    216		val = time_to_val(&t,	LM3533_LED_DELAY3_TMIN,
    217					LM3533_LED_DELAY3_TSTEP,
    218					LM3533_LED_DELAY3_VMIN,
    219					LM3533_LED_DELAY3_VMAX);
    220	} else if (t >= (LM3533_LED_DELAY1_TMAX + LM3533_LED_DELAY2_TMIN) / 2) {
    221		t = clamp(t, LM3533_LED_DELAY2_TMIN, LM3533_LED_DELAY2_TMAX);
    222		val = time_to_val(&t,	LM3533_LED_DELAY2_TMIN,
    223					LM3533_LED_DELAY2_TSTEP,
    224					LM3533_LED_DELAY2_VMIN,
    225					LM3533_LED_DELAY2_VMAX);
    226	} else {
    227		t = clamp(t, LM3533_LED_DELAY1_TMIN, LM3533_LED_DELAY1_TMAX);
    228		val = time_to_val(&t,	LM3533_LED_DELAY1_TMIN,
    229					LM3533_LED_DELAY1_TSTEP,
    230					LM3533_LED_DELAY1_VMIN,
    231					LM3533_LED_DELAY1_VMAX);
    232	}
    233
    234	*delay = (t + 500) / 1000;
    235
    236	return val;
    237}
    238
    239/*
    240 * Set delay register base to *delay (in ms) and update *delay to reflect
    241 * actual hardware delay used.
    242 */
    243static u8 lm3533_led_delay_set(struct lm3533_led *led, u8 base,
    244							unsigned long *delay)
    245{
    246	unsigned t;
    247	u8 val;
    248	u8 reg;
    249	int ret;
    250
    251	t = (unsigned)*delay;
    252
    253	/* Delay group 3 is only available for low time (delay off). */
    254	if (base != LM3533_REG_PATTERN_LOW_TIME_BASE)
    255		t = min(t, LM3533_LED_DELAY2_TMAX / 1000);
    256
    257	val = lm3533_led_get_hw_delay(&t);
    258
    259	dev_dbg(led->cdev.dev, "%s - %lu: %u (0x%02x)\n", __func__,
    260							*delay, t, val);
    261	reg = lm3533_led_get_pattern_reg(led, base);
    262	ret = lm3533_write(led->lm3533, reg, val);
    263	if (ret)
    264		dev_err(led->cdev.dev, "failed to set delay (%02x)\n", reg);
    265
    266	*delay = t;
    267
    268	return ret;
    269}
    270
    271static int lm3533_led_delay_on_set(struct lm3533_led *led, unsigned long *t)
    272{
    273	return lm3533_led_delay_set(led, LM3533_REG_PATTERN_HIGH_TIME_BASE, t);
    274}
    275
    276static int lm3533_led_delay_off_set(struct lm3533_led *led, unsigned long *t)
    277{
    278	return lm3533_led_delay_set(led, LM3533_REG_PATTERN_LOW_TIME_BASE, t);
    279}
    280
    281static int lm3533_led_blink_set(struct led_classdev *cdev,
    282				unsigned long *delay_on,
    283				unsigned long *delay_off)
    284{
    285	struct lm3533_led *led = to_lm3533_led(cdev);
    286	int ret;
    287
    288	dev_dbg(led->cdev.dev, "%s - on = %lu, off = %lu\n", __func__,
    289							*delay_on, *delay_off);
    290
    291	if (*delay_on > LM3533_LED_DELAY_ON_MAX ||
    292					*delay_off > LM3533_LED_DELAY_OFF_MAX)
    293		return -EINVAL;
    294
    295	if (*delay_on == 0 && *delay_off == 0) {
    296		*delay_on = 500;
    297		*delay_off = 500;
    298	}
    299
    300	ret = lm3533_led_delay_on_set(led, delay_on);
    301	if (ret)
    302		return ret;
    303
    304	ret = lm3533_led_delay_off_set(led, delay_off);
    305	if (ret)
    306		return ret;
    307
    308	return lm3533_led_pattern_enable(led, 1);
    309}
    310
    311static ssize_t show_id(struct device *dev,
    312				struct device_attribute *attr, char *buf)
    313{
    314	struct led_classdev *led_cdev = dev_get_drvdata(dev);
    315	struct lm3533_led *led = to_lm3533_led(led_cdev);
    316
    317	return scnprintf(buf, PAGE_SIZE, "%d\n", led->id);
    318}
    319
    320/*
    321 * Pattern generator rise/fall times:
    322 *
    323 *   0 - 2048 us (default)
    324 *   1 - 262 ms
    325 *   2 - 524 ms
    326 *   3 - 1.049 s
    327 *   4 - 2.097 s
    328 *   5 - 4.194 s
    329 *   6 - 8.389 s
    330 *   7 - 16.78 s
    331 */
    332static ssize_t show_risefalltime(struct device *dev,
    333					struct device_attribute *attr,
    334					char *buf, u8 base)
    335{
    336	struct led_classdev *led_cdev = dev_get_drvdata(dev);
    337	struct lm3533_led *led = to_lm3533_led(led_cdev);
    338	ssize_t ret;
    339	u8 reg;
    340	u8 val;
    341
    342	reg = lm3533_led_get_pattern_reg(led, base);
    343	ret = lm3533_read(led->lm3533, reg, &val);
    344	if (ret)
    345		return ret;
    346
    347	return scnprintf(buf, PAGE_SIZE, "%x\n", val);
    348}
    349
    350static ssize_t show_risetime(struct device *dev,
    351				struct device_attribute *attr, char *buf)
    352{
    353	return show_risefalltime(dev, attr, buf,
    354					LM3533_REG_PATTERN_RISETIME_BASE);
    355}
    356
    357static ssize_t show_falltime(struct device *dev,
    358				struct device_attribute *attr, char *buf)
    359{
    360	return show_risefalltime(dev, attr, buf,
    361					LM3533_REG_PATTERN_FALLTIME_BASE);
    362}
    363
    364static ssize_t store_risefalltime(struct device *dev,
    365					struct device_attribute *attr,
    366					const char *buf, size_t len, u8 base)
    367{
    368	struct led_classdev *led_cdev = dev_get_drvdata(dev);
    369	struct lm3533_led *led = to_lm3533_led(led_cdev);
    370	u8 val;
    371	u8 reg;
    372	int ret;
    373
    374	if (kstrtou8(buf, 0, &val) || val > LM3533_RISEFALLTIME_MAX)
    375		return -EINVAL;
    376
    377	reg = lm3533_led_get_pattern_reg(led, base);
    378	ret = lm3533_write(led->lm3533, reg, val);
    379	if (ret)
    380		return ret;
    381
    382	return len;
    383}
    384
    385static ssize_t store_risetime(struct device *dev,
    386					struct device_attribute *attr,
    387					const char *buf, size_t len)
    388{
    389	return store_risefalltime(dev, attr, buf, len,
    390					LM3533_REG_PATTERN_RISETIME_BASE);
    391}
    392
    393static ssize_t store_falltime(struct device *dev,
    394					struct device_attribute *attr,
    395					const char *buf, size_t len)
    396{
    397	return store_risefalltime(dev, attr, buf, len,
    398					LM3533_REG_PATTERN_FALLTIME_BASE);
    399}
    400
    401static ssize_t show_als_channel(struct device *dev,
    402				struct device_attribute *attr, char *buf)
    403{
    404	struct led_classdev *led_cdev = dev_get_drvdata(dev);
    405	struct lm3533_led *led = to_lm3533_led(led_cdev);
    406	unsigned channel;
    407	u8 reg;
    408	u8 val;
    409	int ret;
    410
    411	reg = lm3533_led_get_lv_reg(led, LM3533_REG_CTRLBANK_BCONF_BASE);
    412	ret = lm3533_read(led->lm3533, reg, &val);
    413	if (ret)
    414		return ret;
    415
    416	channel = (val & LM3533_REG_CTRLBANK_BCONF_ALS_CHANNEL_MASK) + 1;
    417
    418	return scnprintf(buf, PAGE_SIZE, "%u\n", channel);
    419}
    420
    421static ssize_t store_als_channel(struct device *dev,
    422					struct device_attribute *attr,
    423					const char *buf, size_t len)
    424{
    425	struct led_classdev *led_cdev = dev_get_drvdata(dev);
    426	struct lm3533_led *led = to_lm3533_led(led_cdev);
    427	unsigned channel;
    428	u8 reg;
    429	u8 val;
    430	u8 mask;
    431	int ret;
    432
    433	if (kstrtouint(buf, 0, &channel))
    434		return -EINVAL;
    435
    436	if (channel < LM3533_ALS_CHANNEL_LV_MIN ||
    437					channel > LM3533_ALS_CHANNEL_LV_MAX)
    438		return -EINVAL;
    439
    440	reg = lm3533_led_get_lv_reg(led, LM3533_REG_CTRLBANK_BCONF_BASE);
    441	mask = LM3533_REG_CTRLBANK_BCONF_ALS_CHANNEL_MASK;
    442	val = channel - 1;
    443
    444	ret = lm3533_update(led->lm3533, reg, val, mask);
    445	if (ret)
    446		return ret;
    447
    448	return len;
    449}
    450
    451static ssize_t show_als_en(struct device *dev,
    452				struct device_attribute *attr, char *buf)
    453{
    454	struct led_classdev *led_cdev = dev_get_drvdata(dev);
    455	struct lm3533_led *led = to_lm3533_led(led_cdev);
    456	bool enable;
    457	u8 reg;
    458	u8 val;
    459	int ret;
    460
    461	reg = lm3533_led_get_lv_reg(led, LM3533_REG_CTRLBANK_BCONF_BASE);
    462	ret = lm3533_read(led->lm3533, reg, &val);
    463	if (ret)
    464		return ret;
    465
    466	enable = val & LM3533_REG_CTRLBANK_BCONF_ALS_EN_MASK;
    467
    468	return scnprintf(buf, PAGE_SIZE, "%d\n", enable);
    469}
    470
    471static ssize_t store_als_en(struct device *dev,
    472					struct device_attribute *attr,
    473					const char *buf, size_t len)
    474{
    475	struct led_classdev *led_cdev = dev_get_drvdata(dev);
    476	struct lm3533_led *led = to_lm3533_led(led_cdev);
    477	unsigned enable;
    478	u8 reg;
    479	u8 mask;
    480	u8 val;
    481	int ret;
    482
    483	if (kstrtouint(buf, 0, &enable))
    484		return -EINVAL;
    485
    486	reg = lm3533_led_get_lv_reg(led, LM3533_REG_CTRLBANK_BCONF_BASE);
    487	mask = LM3533_REG_CTRLBANK_BCONF_ALS_EN_MASK;
    488
    489	if (enable)
    490		val = mask;
    491	else
    492		val = 0;
    493
    494	ret = lm3533_update(led->lm3533, reg, val, mask);
    495	if (ret)
    496		return ret;
    497
    498	return len;
    499}
    500
    501static ssize_t show_linear(struct device *dev,
    502				struct device_attribute *attr, char *buf)
    503{
    504	struct led_classdev *led_cdev = dev_get_drvdata(dev);
    505	struct lm3533_led *led = to_lm3533_led(led_cdev);
    506	u8 reg;
    507	u8 val;
    508	int linear;
    509	int ret;
    510
    511	reg = lm3533_led_get_lv_reg(led, LM3533_REG_CTRLBANK_BCONF_BASE);
    512	ret = lm3533_read(led->lm3533, reg, &val);
    513	if (ret)
    514		return ret;
    515
    516	if (val & LM3533_REG_CTRLBANK_BCONF_MAPPING_MASK)
    517		linear = 1;
    518	else
    519		linear = 0;
    520
    521	return scnprintf(buf, PAGE_SIZE, "%x\n", linear);
    522}
    523
    524static ssize_t store_linear(struct device *dev,
    525					struct device_attribute *attr,
    526					const char *buf, size_t len)
    527{
    528	struct led_classdev *led_cdev = dev_get_drvdata(dev);
    529	struct lm3533_led *led = to_lm3533_led(led_cdev);
    530	unsigned long linear;
    531	u8 reg;
    532	u8 mask;
    533	u8 val;
    534	int ret;
    535
    536	if (kstrtoul(buf, 0, &linear))
    537		return -EINVAL;
    538
    539	reg = lm3533_led_get_lv_reg(led, LM3533_REG_CTRLBANK_BCONF_BASE);
    540	mask = LM3533_REG_CTRLBANK_BCONF_MAPPING_MASK;
    541
    542	if (linear)
    543		val = mask;
    544	else
    545		val = 0;
    546
    547	ret = lm3533_update(led->lm3533, reg, val, mask);
    548	if (ret)
    549		return ret;
    550
    551	return len;
    552}
    553
    554static ssize_t show_pwm(struct device *dev,
    555					struct device_attribute *attr,
    556					char *buf)
    557{
    558	struct led_classdev *led_cdev = dev_get_drvdata(dev);
    559	struct lm3533_led *led = to_lm3533_led(led_cdev);
    560	u8 val;
    561	int ret;
    562
    563	ret = lm3533_ctrlbank_get_pwm(&led->cb, &val);
    564	if (ret)
    565		return ret;
    566
    567	return scnprintf(buf, PAGE_SIZE, "%u\n", val);
    568}
    569
    570static ssize_t store_pwm(struct device *dev,
    571					struct device_attribute *attr,
    572					const char *buf, size_t len)
    573{
    574	struct led_classdev *led_cdev = dev_get_drvdata(dev);
    575	struct lm3533_led *led = to_lm3533_led(led_cdev);
    576	u8 val;
    577	int ret;
    578
    579	if (kstrtou8(buf, 0, &val))
    580		return -EINVAL;
    581
    582	ret = lm3533_ctrlbank_set_pwm(&led->cb, val);
    583	if (ret)
    584		return ret;
    585
    586	return len;
    587}
    588
    589static LM3533_ATTR_RW(als_channel);
    590static LM3533_ATTR_RW(als_en);
    591static LM3533_ATTR_RW(falltime);
    592static LM3533_ATTR_RO(id);
    593static LM3533_ATTR_RW(linear);
    594static LM3533_ATTR_RW(pwm);
    595static LM3533_ATTR_RW(risetime);
    596
    597static struct attribute *lm3533_led_attributes[] = {
    598	&dev_attr_als_channel.attr,
    599	&dev_attr_als_en.attr,
    600	&dev_attr_falltime.attr,
    601	&dev_attr_id.attr,
    602	&dev_attr_linear.attr,
    603	&dev_attr_pwm.attr,
    604	&dev_attr_risetime.attr,
    605	NULL,
    606};
    607
    608static umode_t lm3533_led_attr_is_visible(struct kobject *kobj,
    609					     struct attribute *attr, int n)
    610{
    611	struct device *dev = kobj_to_dev(kobj);
    612	struct led_classdev *led_cdev = dev_get_drvdata(dev);
    613	struct lm3533_led *led = to_lm3533_led(led_cdev);
    614	umode_t mode = attr->mode;
    615
    616	if (attr == &dev_attr_als_channel.attr ||
    617					attr == &dev_attr_als_en.attr) {
    618		if (!led->lm3533->have_als)
    619			mode = 0;
    620	}
    621
    622	return mode;
    623};
    624
    625static const struct attribute_group lm3533_led_attribute_group = {
    626	.is_visible	= lm3533_led_attr_is_visible,
    627	.attrs		= lm3533_led_attributes
    628};
    629
    630static const struct attribute_group *lm3533_led_attribute_groups[] = {
    631	&lm3533_led_attribute_group,
    632	NULL
    633};
    634
    635static int lm3533_led_setup(struct lm3533_led *led,
    636					struct lm3533_led_platform_data *pdata)
    637{
    638	int ret;
    639
    640	ret = lm3533_ctrlbank_set_max_current(&led->cb, pdata->max_current);
    641	if (ret)
    642		return ret;
    643
    644	return lm3533_ctrlbank_set_pwm(&led->cb, pdata->pwm);
    645}
    646
    647static int lm3533_led_probe(struct platform_device *pdev)
    648{
    649	struct lm3533 *lm3533;
    650	struct lm3533_led_platform_data *pdata;
    651	struct lm3533_led *led;
    652	int ret;
    653
    654	dev_dbg(&pdev->dev, "%s\n", __func__);
    655
    656	lm3533 = dev_get_drvdata(pdev->dev.parent);
    657	if (!lm3533)
    658		return -EINVAL;
    659
    660	pdata = dev_get_platdata(&pdev->dev);
    661	if (!pdata) {
    662		dev_err(&pdev->dev, "no platform data\n");
    663		return -EINVAL;
    664	}
    665
    666	if (pdev->id < 0 || pdev->id >= LM3533_LVCTRLBANK_COUNT) {
    667		dev_err(&pdev->dev, "illegal LED id %d\n", pdev->id);
    668		return -EINVAL;
    669	}
    670
    671	led = devm_kzalloc(&pdev->dev, sizeof(*led), GFP_KERNEL);
    672	if (!led)
    673		return -ENOMEM;
    674
    675	led->lm3533 = lm3533;
    676	led->cdev.name = pdata->name;
    677	led->cdev.default_trigger = pdata->default_trigger;
    678	led->cdev.brightness_set_blocking = lm3533_led_set;
    679	led->cdev.brightness_get = lm3533_led_get;
    680	led->cdev.blink_set = lm3533_led_blink_set;
    681	led->cdev.brightness = LED_OFF;
    682	led->cdev.groups = lm3533_led_attribute_groups;
    683	led->id = pdev->id;
    684
    685	mutex_init(&led->mutex);
    686
    687	/* The class framework makes a callback to get brightness during
    688	 * registration so use parent device (for error reporting) until
    689	 * registered.
    690	 */
    691	led->cb.lm3533 = lm3533;
    692	led->cb.id = lm3533_led_get_ctrlbank_id(led);
    693	led->cb.dev = lm3533->dev;
    694
    695	platform_set_drvdata(pdev, led);
    696
    697	ret = led_classdev_register(pdev->dev.parent, &led->cdev);
    698	if (ret) {
    699		dev_err(&pdev->dev, "failed to register LED %d\n", pdev->id);
    700		return ret;
    701	}
    702
    703	led->cb.dev = led->cdev.dev;
    704
    705	ret = lm3533_led_setup(led, pdata);
    706	if (ret)
    707		goto err_deregister;
    708
    709	ret = lm3533_ctrlbank_enable(&led->cb);
    710	if (ret)
    711		goto err_deregister;
    712
    713	return 0;
    714
    715err_deregister:
    716	led_classdev_unregister(&led->cdev);
    717
    718	return ret;
    719}
    720
    721static int lm3533_led_remove(struct platform_device *pdev)
    722{
    723	struct lm3533_led *led = platform_get_drvdata(pdev);
    724
    725	dev_dbg(&pdev->dev, "%s\n", __func__);
    726
    727	lm3533_ctrlbank_disable(&led->cb);
    728	led_classdev_unregister(&led->cdev);
    729
    730	return 0;
    731}
    732
    733static void lm3533_led_shutdown(struct platform_device *pdev)
    734{
    735
    736	struct lm3533_led *led = platform_get_drvdata(pdev);
    737
    738	dev_dbg(&pdev->dev, "%s\n", __func__);
    739
    740	lm3533_ctrlbank_disable(&led->cb);
    741	lm3533_led_set(&led->cdev, LED_OFF);		/* disable blink */
    742}
    743
    744static struct platform_driver lm3533_led_driver = {
    745	.driver = {
    746		.name = "lm3533-leds",
    747	},
    748	.probe		= lm3533_led_probe,
    749	.remove		= lm3533_led_remove,
    750	.shutdown	= lm3533_led_shutdown,
    751};
    752module_platform_driver(lm3533_led_driver);
    753
    754MODULE_AUTHOR("Johan Hovold <jhovold@gmail.com>");
    755MODULE_DESCRIPTION("LM3533 LED driver");
    756MODULE_LICENSE("GPL");
    757MODULE_ALIAS("platform:lm3533-leds");