2 * lm3533-core.c -- LM3533 Core
4 * Copyright (C) 2011-2012 Texas Instruments
6 * Author: Johan Hovold <jhovold@gmail.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/err.h>
18 #include <linux/gpio.h>
19 #include <linux/i2c.h>
20 #include <linux/mfd/core.h>
21 #include <linux/regmap.h>
22 #include <linux/seq_file.h>
23 #include <linux/slab.h>
24 #include <linux/uaccess.h>
26 #include <linux/mfd/lm3533.h>
29 #define LM3533_BOOST_OVP_MASK 0x06
30 #define LM3533_BOOST_OVP_SHIFT 1
32 #define LM3533_BOOST_FREQ_MASK 0x01
33 #define LM3533_BOOST_FREQ_SHIFT 0
35 #define LM3533_BL_ID_MASK 1
36 #define LM3533_LED_ID_MASK 3
37 #define LM3533_BL_ID_MAX 1
38 #define LM3533_LED_ID_MAX 3
40 #define LM3533_HVLED_ID_MAX 2
41 #define LM3533_LVLED_ID_MAX 5
43 #define LM3533_REG_OUTPUT_CONF1 0x10
44 #define LM3533_REG_OUTPUT_CONF2 0x11
45 #define LM3533_REG_BOOST_PWM 0x2c
47 #define LM3533_REG_MAX 0xb2
50 static struct mfd_cell lm3533_als_devs
[] = {
57 static struct mfd_cell lm3533_bl_devs
[] = {
59 .name
= "lm3533-backlight",
63 .name
= "lm3533-backlight",
68 static struct mfd_cell lm3533_led_devs
[] = {
70 .name
= "lm3533-leds",
74 .name
= "lm3533-leds",
78 .name
= "lm3533-leds",
82 .name
= "lm3533-leds",
87 int lm3533_read(struct lm3533
*lm3533
, u8 reg
, u8
*val
)
92 ret
= regmap_read(lm3533
->regmap
, reg
, &tmp
);
94 dev_err(lm3533
->dev
, "failed to read register %02x: %d\n",
101 dev_dbg(lm3533
->dev
, "read [%02x]: %02x\n", reg
, *val
);
105 EXPORT_SYMBOL_GPL(lm3533_read
);
107 int lm3533_write(struct lm3533
*lm3533
, u8 reg
, u8 val
)
111 dev_dbg(lm3533
->dev
, "write [%02x]: %02x\n", reg
, val
);
113 ret
= regmap_write(lm3533
->regmap
, reg
, val
);
115 dev_err(lm3533
->dev
, "failed to write register %02x: %d\n",
121 EXPORT_SYMBOL_GPL(lm3533_write
);
123 int lm3533_update(struct lm3533
*lm3533
, u8 reg
, u8 val
, u8 mask
)
127 dev_dbg(lm3533
->dev
, "update [%02x]: %02x/%02x\n", reg
, val
, mask
);
129 ret
= regmap_update_bits(lm3533
->regmap
, reg
, mask
, val
);
131 dev_err(lm3533
->dev
, "failed to update register %02x: %d\n",
137 EXPORT_SYMBOL_GPL(lm3533_update
);
139 static int lm3533_set_boost_freq(struct lm3533
*lm3533
,
140 enum lm3533_boost_freq freq
)
144 ret
= lm3533_update(lm3533
, LM3533_REG_BOOST_PWM
,
145 freq
<< LM3533_BOOST_FREQ_SHIFT
,
146 LM3533_BOOST_FREQ_MASK
);
148 dev_err(lm3533
->dev
, "failed to set boost frequency\n");
154 static int lm3533_set_boost_ovp(struct lm3533
*lm3533
,
155 enum lm3533_boost_ovp ovp
)
159 ret
= lm3533_update(lm3533
, LM3533_REG_BOOST_PWM
,
160 ovp
<< LM3533_BOOST_OVP_SHIFT
,
161 LM3533_BOOST_OVP_MASK
);
163 dev_err(lm3533
->dev
, "failed to set boost ovp\n");
169 * HVLED output config -- output hvled controlled by backlight bl
171 static int lm3533_set_hvled_config(struct lm3533
*lm3533
, u8 hvled
, u8 bl
)
178 if (hvled
== 0 || hvled
> LM3533_HVLED_ID_MAX
)
181 if (bl
> LM3533_BL_ID_MAX
)
185 mask
= LM3533_BL_ID_MASK
<< shift
;
188 ret
= lm3533_update(lm3533
, LM3533_REG_OUTPUT_CONF1
, val
, mask
);
190 dev_err(lm3533
->dev
, "failed to set hvled config\n");
196 * LVLED output config -- output lvled controlled by LED led
198 static int lm3533_set_lvled_config(struct lm3533
*lm3533
, u8 lvled
, u8 led
)
206 if (lvled
== 0 || lvled
> LM3533_LVLED_ID_MAX
)
209 if (led
> LM3533_LED_ID_MAX
)
213 reg
= LM3533_REG_OUTPUT_CONF1
;
216 reg
= LM3533_REG_OUTPUT_CONF2
;
217 shift
= 2 * (lvled
- 4);
220 mask
= LM3533_LED_ID_MASK
<< shift
;
223 ret
= lm3533_update(lm3533
, reg
, val
, mask
);
225 dev_err(lm3533
->dev
, "failed to set lvled config\n");
230 static void lm3533_enable(struct lm3533
*lm3533
)
232 if (gpio_is_valid(lm3533
->gpio_hwen
))
233 gpio_set_value(lm3533
->gpio_hwen
, 1);
236 static void lm3533_disable(struct lm3533
*lm3533
)
238 if (gpio_is_valid(lm3533
->gpio_hwen
))
239 gpio_set_value(lm3533
->gpio_hwen
, 0);
242 enum lm3533_attribute_type
{
243 LM3533_ATTR_TYPE_BACKLIGHT
,
244 LM3533_ATTR_TYPE_LED
,
247 struct lm3533_device_attribute
{
248 struct device_attribute dev_attr
;
249 enum lm3533_attribute_type type
;
257 #define to_lm3533_dev_attr(_attr) \
258 container_of(_attr, struct lm3533_device_attribute, dev_attr)
260 static ssize_t
show_output(struct device
*dev
,
261 struct device_attribute
*attr
, char *buf
)
263 struct lm3533
*lm3533
= dev_get_drvdata(dev
);
264 struct lm3533_device_attribute
*lattr
= to_lm3533_dev_attr(attr
);
265 int id
= lattr
->u
.output
.id
;
272 if (lattr
->type
== LM3533_ATTR_TYPE_BACKLIGHT
) {
273 reg
= LM3533_REG_OUTPUT_CONF1
;
275 mask
= LM3533_BL_ID_MASK
<< shift
;
278 reg
= LM3533_REG_OUTPUT_CONF1
;
281 reg
= LM3533_REG_OUTPUT_CONF2
;
282 shift
= 2 * (id
- 4);
284 mask
= LM3533_LED_ID_MASK
<< shift
;
287 ret
= lm3533_read(lm3533
, reg
, &val
);
291 val
= (val
& mask
) >> shift
;
293 return scnprintf(buf
, PAGE_SIZE
, "%u\n", val
);
296 static ssize_t
store_output(struct device
*dev
,
297 struct device_attribute
*attr
,
298 const char *buf
, size_t len
)
300 struct lm3533
*lm3533
= dev_get_drvdata(dev
);
301 struct lm3533_device_attribute
*lattr
= to_lm3533_dev_attr(attr
);
302 int id
= lattr
->u
.output
.id
;
306 if (kstrtou8(buf
, 0, &val
))
309 if (lattr
->type
== LM3533_ATTR_TYPE_BACKLIGHT
)
310 ret
= lm3533_set_hvled_config(lm3533
, id
, val
);
312 ret
= lm3533_set_lvled_config(lm3533
, id
, val
);
320 #define LM3533_OUTPUT_ATTR(_name, _mode, _show, _store, _type, _id) \
321 struct lm3533_device_attribute lm3533_dev_attr_##_name = \
322 { .dev_attr = __ATTR(_name, _mode, _show, _store), \
324 .u.output = { .id = _id }, }
326 #define LM3533_OUTPUT_ATTR_RW(_name, _type, _id) \
327 LM3533_OUTPUT_ATTR(output_##_name, S_IRUGO | S_IWUSR, \
328 show_output, store_output, _type, _id)
330 #define LM3533_OUTPUT_HVLED_ATTR_RW(_nr) \
331 LM3533_OUTPUT_ATTR_RW(hvled##_nr, LM3533_ATTR_TYPE_BACKLIGHT, _nr)
332 #define LM3533_OUTPUT_LVLED_ATTR_RW(_nr) \
333 LM3533_OUTPUT_ATTR_RW(lvled##_nr, LM3533_ATTR_TYPE_LED, _nr)
337 * output_hvled<nr> 0-1
338 * output_lvled<nr> 0-3
340 static LM3533_OUTPUT_HVLED_ATTR_RW(1);
341 static LM3533_OUTPUT_HVLED_ATTR_RW(2);
342 static LM3533_OUTPUT_LVLED_ATTR_RW(1);
343 static LM3533_OUTPUT_LVLED_ATTR_RW(2);
344 static LM3533_OUTPUT_LVLED_ATTR_RW(3);
345 static LM3533_OUTPUT_LVLED_ATTR_RW(4);
346 static LM3533_OUTPUT_LVLED_ATTR_RW(5);
348 static struct attribute
*lm3533_attributes
[] = {
349 &lm3533_dev_attr_output_hvled1
.dev_attr
.attr
,
350 &lm3533_dev_attr_output_hvled2
.dev_attr
.attr
,
351 &lm3533_dev_attr_output_lvled1
.dev_attr
.attr
,
352 &lm3533_dev_attr_output_lvled2
.dev_attr
.attr
,
353 &lm3533_dev_attr_output_lvled3
.dev_attr
.attr
,
354 &lm3533_dev_attr_output_lvled4
.dev_attr
.attr
,
355 &lm3533_dev_attr_output_lvled5
.dev_attr
.attr
,
359 #define to_dev_attr(_attr) \
360 container_of(_attr, struct device_attribute, attr)
362 static umode_t
lm3533_attr_is_visible(struct kobject
*kobj
,
363 struct attribute
*attr
, int n
)
365 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
366 struct lm3533
*lm3533
= dev_get_drvdata(dev
);
367 struct device_attribute
*dattr
= to_dev_attr(attr
);
368 struct lm3533_device_attribute
*lattr
= to_lm3533_dev_attr(dattr
);
369 enum lm3533_attribute_type type
= lattr
->type
;
370 umode_t mode
= attr
->mode
;
372 if (!lm3533
->have_backlights
&& type
== LM3533_ATTR_TYPE_BACKLIGHT
)
374 else if (!lm3533
->have_leds
&& type
== LM3533_ATTR_TYPE_LED
)
380 static struct attribute_group lm3533_attribute_group
= {
381 .is_visible
= lm3533_attr_is_visible
,
382 .attrs
= lm3533_attributes
385 static int __devinit
lm3533_device_als_init(struct lm3533
*lm3533
)
387 struct lm3533_platform_data
*pdata
= lm3533
->dev
->platform_data
;
393 lm3533_als_devs
[0].platform_data
= pdata
->als
;
394 lm3533_als_devs
[0].pdata_size
= sizeof(*pdata
->als
);
396 ret
= mfd_add_devices(lm3533
->dev
, 0, lm3533_als_devs
, 1, NULL
, 0);
398 dev_err(lm3533
->dev
, "failed to add ALS device\n");
402 lm3533
->have_als
= 1;
407 static int __devinit
lm3533_device_bl_init(struct lm3533
*lm3533
)
409 struct lm3533_platform_data
*pdata
= lm3533
->dev
->platform_data
;
413 if (!pdata
->backlights
|| pdata
->num_backlights
== 0)
416 if (pdata
->num_backlights
> ARRAY_SIZE(lm3533_bl_devs
))
417 pdata
->num_backlights
= ARRAY_SIZE(lm3533_bl_devs
);
419 for (i
= 0; i
< pdata
->num_backlights
; ++i
) {
420 lm3533_bl_devs
[i
].platform_data
= &pdata
->backlights
[i
];
421 lm3533_bl_devs
[i
].pdata_size
= sizeof(pdata
->backlights
[i
]);
424 ret
= mfd_add_devices(lm3533
->dev
, 0, lm3533_bl_devs
,
425 pdata
->num_backlights
, NULL
, 0);
427 dev_err(lm3533
->dev
, "failed to add backlight devices\n");
431 lm3533
->have_backlights
= 1;
436 static int __devinit
lm3533_device_led_init(struct lm3533
*lm3533
)
438 struct lm3533_platform_data
*pdata
= lm3533
->dev
->platform_data
;
442 if (!pdata
->leds
|| pdata
->num_leds
== 0)
445 if (pdata
->num_leds
> ARRAY_SIZE(lm3533_led_devs
))
446 pdata
->num_leds
= ARRAY_SIZE(lm3533_led_devs
);
448 for (i
= 0; i
< pdata
->num_leds
; ++i
) {
449 lm3533_led_devs
[i
].platform_data
= &pdata
->leds
[i
];
450 lm3533_led_devs
[i
].pdata_size
= sizeof(pdata
->leds
[i
]);
453 ret
= mfd_add_devices(lm3533
->dev
, 0, lm3533_led_devs
,
454 pdata
->num_leds
, NULL
, 0);
456 dev_err(lm3533
->dev
, "failed to add LED devices\n");
460 lm3533
->have_leds
= 1;
465 static int __devinit
lm3533_device_setup(struct lm3533
*lm3533
,
466 struct lm3533_platform_data
*pdata
)
470 ret
= lm3533_set_boost_freq(lm3533
, pdata
->boost_freq
);
474 ret
= lm3533_set_boost_ovp(lm3533
, pdata
->boost_ovp
);
481 static int __devinit
lm3533_device_init(struct lm3533
*lm3533
)
483 struct lm3533_platform_data
*pdata
= lm3533
->dev
->platform_data
;
486 dev_dbg(lm3533
->dev
, "%s\n", __func__
);
489 dev_err(lm3533
->dev
, "no platform data\n");
493 lm3533
->gpio_hwen
= pdata
->gpio_hwen
;
495 dev_set_drvdata(lm3533
->dev
, lm3533
);
497 if (gpio_is_valid(lm3533
->gpio_hwen
)) {
498 ret
= gpio_request_one(lm3533
->gpio_hwen
, GPIOF_OUT_INIT_LOW
,
502 "failed to request HWEN GPIO %d\n",
508 lm3533_enable(lm3533
);
510 ret
= lm3533_device_setup(lm3533
, pdata
);
514 lm3533_device_als_init(lm3533
);
515 lm3533_device_bl_init(lm3533
);
516 lm3533_device_led_init(lm3533
);
518 ret
= sysfs_create_group(&lm3533
->dev
->kobj
, &lm3533_attribute_group
);
520 dev_err(lm3533
->dev
, "failed to create sysfs attributes\n");
527 mfd_remove_devices(lm3533
->dev
);
529 lm3533_disable(lm3533
);
530 if (gpio_is_valid(lm3533
->gpio_hwen
))
531 gpio_free(lm3533
->gpio_hwen
);
536 static void __devexit
lm3533_device_exit(struct lm3533
*lm3533
)
538 dev_dbg(lm3533
->dev
, "%s\n", __func__
);
540 sysfs_remove_group(&lm3533
->dev
->kobj
, &lm3533_attribute_group
);
542 mfd_remove_devices(lm3533
->dev
);
543 lm3533_disable(lm3533
);
544 if (gpio_is_valid(lm3533
->gpio_hwen
))
545 gpio_free(lm3533
->gpio_hwen
);
548 static bool lm3533_readable_register(struct device
*dev
, unsigned int reg
)
567 static bool lm3533_volatile_register(struct device
*dev
, unsigned int reg
)
570 case 0x34 ... 0x36: /* zone */
571 case 0x37 ... 0x38: /* adc */
572 case 0xb0 ... 0xb1: /* fault */
579 static bool lm3533_precious_register(struct device
*dev
, unsigned int reg
)
582 case 0x34: /* zone */
589 static struct regmap_config regmap_config
= {
592 .max_register
= LM3533_REG_MAX
,
593 .readable_reg
= lm3533_readable_register
,
594 .volatile_reg
= lm3533_volatile_register
,
595 .precious_reg
= lm3533_precious_register
,
598 static int __devinit
lm3533_i2c_probe(struct i2c_client
*i2c
,
599 const struct i2c_device_id
*id
)
601 struct lm3533
*lm3533
;
604 dev_dbg(&i2c
->dev
, "%s\n", __func__
);
606 lm3533
= devm_kzalloc(&i2c
->dev
, sizeof(*lm3533
), GFP_KERNEL
);
610 i2c_set_clientdata(i2c
, lm3533
);
612 lm3533
->regmap
= devm_regmap_init_i2c(i2c
, ®map_config
);
613 if (IS_ERR(lm3533
->regmap
))
614 return PTR_ERR(lm3533
->regmap
);
616 lm3533
->dev
= &i2c
->dev
;
617 lm3533
->irq
= i2c
->irq
;
619 ret
= lm3533_device_init(lm3533
);
626 static int __devexit
lm3533_i2c_remove(struct i2c_client
*i2c
)
628 struct lm3533
*lm3533
= i2c_get_clientdata(i2c
);
630 dev_dbg(&i2c
->dev
, "%s\n", __func__
);
632 lm3533_device_exit(lm3533
);
637 static const struct i2c_device_id lm3533_i2c_ids
[] = {
641 MODULE_DEVICE_TABLE(i2c
, lm3533_i2c_ids
);
643 static struct i2c_driver lm3533_i2c_driver
= {
646 .owner
= THIS_MODULE
,
648 .id_table
= lm3533_i2c_ids
,
649 .probe
= lm3533_i2c_probe
,
650 .remove
= __devexit_p(lm3533_i2c_remove
),
653 static int __init
lm3533_i2c_init(void)
655 return i2c_add_driver(&lm3533_i2c_driver
);
657 subsys_initcall(lm3533_i2c_init
);
659 static void __exit
lm3533_i2c_exit(void)
661 i2c_del_driver(&lm3533_i2c_driver
);
663 module_exit(lm3533_i2c_exit
);
665 MODULE_AUTHOR("Johan Hovold <jhovold@gmail.com>");
666 MODULE_DESCRIPTION("LM3533 Core");
667 MODULE_LICENSE("GPL");