Linux 4.1.16
[linux/fpc-iii.git] / drivers / mfd / lp3943.c
blob335b930112b27fadda90533d3d9aacfc9619ef1d
1 /*
2 * TI/National Semiconductor LP3943 MFD Core Driver
4 * Copyright 2013 Texas Instruments
6 * Author: Milo Kim <milo.kim@ti.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * Driver structure:
13 * LP3943 is an integrated device capable of driving 16 output channels.
14 * It can be used for a GPIO expander and PWM generators.
16 * LED control General usage for a device
17 * ___________ ____________________________
19 * LP3943 MFD ---- GPIO expander leds-gpio eg) HW enable pin
20 * |
21 * --- PWM generator leds-pwm eg) PWM input
23 * Internal two PWM channels are used for LED dimming effect.
24 * And each output pin can be used as a GPIO as well.
25 * The LED functionality can work with GPIOs or PWMs.
26 * LEDs can be controlled with legacy leds-gpio(static brightness) or
27 * leds-pwm drivers(dynamic brightness control).
28 * Alternatively, it can be used for generic GPIO and PWM controller.
29 * For example, a GPIO is HW enable pin of a device.
30 * A PWM is input pin of a backlight device.
33 #include <linux/err.h>
34 #include <linux/gpio.h>
35 #include <linux/i2c.h>
36 #include <linux/mfd/core.h>
37 #include <linux/mfd/lp3943.h>
38 #include <linux/module.h>
39 #include <linux/of.h>
40 #include <linux/slab.h>
42 #define LP3943_MAX_REGISTERS 0x09
44 /* Register configuration for pin MUX */
45 static const struct lp3943_reg_cfg lp3943_mux_cfg[] = {
46 /* address, mask, shift */
47 { LP3943_REG_MUX0, 0x03, 0 },
48 { LP3943_REG_MUX0, 0x0C, 2 },
49 { LP3943_REG_MUX0, 0x30, 4 },
50 { LP3943_REG_MUX0, 0xC0, 6 },
51 { LP3943_REG_MUX1, 0x03, 0 },
52 { LP3943_REG_MUX1, 0x0C, 2 },
53 { LP3943_REG_MUX1, 0x30, 4 },
54 { LP3943_REG_MUX1, 0xC0, 6 },
55 { LP3943_REG_MUX2, 0x03, 0 },
56 { LP3943_REG_MUX2, 0x0C, 2 },
57 { LP3943_REG_MUX2, 0x30, 4 },
58 { LP3943_REG_MUX2, 0xC0, 6 },
59 { LP3943_REG_MUX3, 0x03, 0 },
60 { LP3943_REG_MUX3, 0x0C, 2 },
61 { LP3943_REG_MUX3, 0x30, 4 },
62 { LP3943_REG_MUX3, 0xC0, 6 },
65 static const struct mfd_cell lp3943_devs[] = {
67 .name = "lp3943-pwm",
68 .of_compatible = "ti,lp3943-pwm",
71 .name = "lp3943-gpio",
72 .of_compatible = "ti,lp3943-gpio",
76 int lp3943_read_byte(struct lp3943 *lp3943, u8 reg, u8 *read)
78 int ret;
79 unsigned int val;
81 ret = regmap_read(lp3943->regmap, reg, &val);
82 if (ret < 0)
83 return ret;
85 *read = (u8)val;
86 return 0;
88 EXPORT_SYMBOL_GPL(lp3943_read_byte);
90 int lp3943_write_byte(struct lp3943 *lp3943, u8 reg, u8 data)
92 return regmap_write(lp3943->regmap, reg, data);
94 EXPORT_SYMBOL_GPL(lp3943_write_byte);
96 int lp3943_update_bits(struct lp3943 *lp3943, u8 reg, u8 mask, u8 data)
98 return regmap_update_bits(lp3943->regmap, reg, mask, data);
100 EXPORT_SYMBOL_GPL(lp3943_update_bits);
102 static const struct regmap_config lp3943_regmap_config = {
103 .reg_bits = 8,
104 .val_bits = 8,
105 .max_register = LP3943_MAX_REGISTERS,
108 static int lp3943_probe(struct i2c_client *cl, const struct i2c_device_id *id)
110 struct lp3943 *lp3943;
111 struct device *dev = &cl->dev;
113 lp3943 = devm_kzalloc(dev, sizeof(*lp3943), GFP_KERNEL);
114 if (!lp3943)
115 return -ENOMEM;
117 lp3943->regmap = devm_regmap_init_i2c(cl, &lp3943_regmap_config);
118 if (IS_ERR(lp3943->regmap))
119 return PTR_ERR(lp3943->regmap);
121 lp3943->pdata = dev_get_platdata(dev);
122 lp3943->dev = dev;
123 lp3943->mux_cfg = lp3943_mux_cfg;
124 i2c_set_clientdata(cl, lp3943);
126 return mfd_add_devices(dev, -1, lp3943_devs, ARRAY_SIZE(lp3943_devs),
127 NULL, 0, NULL);
130 static int lp3943_remove(struct i2c_client *cl)
132 struct lp3943 *lp3943 = i2c_get_clientdata(cl);
134 mfd_remove_devices(lp3943->dev);
135 return 0;
138 static const struct i2c_device_id lp3943_ids[] = {
139 { "lp3943", 0 },
142 MODULE_DEVICE_TABLE(i2c, lp3943_ids);
144 #ifdef CONFIG_OF
145 static const struct of_device_id lp3943_of_match[] = {
146 { .compatible = "ti,lp3943", },
149 MODULE_DEVICE_TABLE(of, lp3943_of_match);
150 #endif
152 static struct i2c_driver lp3943_driver = {
153 .probe = lp3943_probe,
154 .remove = lp3943_remove,
155 .driver = {
156 .name = "lp3943",
157 .owner = THIS_MODULE,
158 .of_match_table = of_match_ptr(lp3943_of_match),
160 .id_table = lp3943_ids,
163 module_i2c_driver(lp3943_driver);
165 MODULE_DESCRIPTION("LP3943 MFD Core Driver");
166 MODULE_AUTHOR("Milo Kim");
167 MODULE_LICENSE("GPL");