xref: /linux/drivers/mfd/lp3943.c (revision a2cce7a9f1b8cc3d4edce106fb971529f1d4d9ce)
1 /*
2  * TI/National Semiconductor LP3943 MFD Core Driver
3  *
4  * Copyright 2013 Texas Instruments
5  *
6  * Author: Milo Kim <milo.kim@ti.com>
7  *
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.
11  *
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.
15  *
16  *                                   LED control    General usage for a device
17  *                                   ___________   ____________________________
18  *
19  *   LP3943 MFD ---- GPIO expander    leds-gpio        eg) HW enable pin
20  *               |
21  *               --- PWM generator    leds-pwm         eg) PWM input
22  *
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.
31  */
32 
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>
41 
42 #define LP3943_MAX_REGISTERS		0x09
43 
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 },
63 };
64 
65 static const struct mfd_cell lp3943_devs[] = {
66 	{
67 		.name = "lp3943-pwm",
68 		.of_compatible = "ti,lp3943-pwm",
69 	},
70 	{
71 		.name = "lp3943-gpio",
72 		.of_compatible = "ti,lp3943-gpio",
73 	},
74 };
75 
76 int lp3943_read_byte(struct lp3943 *lp3943, u8 reg, u8 *read)
77 {
78 	int ret;
79 	unsigned int val;
80 
81 	ret = regmap_read(lp3943->regmap, reg, &val);
82 	if (ret < 0)
83 		return ret;
84 
85 	*read = (u8)val;
86 	return 0;
87 }
88 EXPORT_SYMBOL_GPL(lp3943_read_byte);
89 
90 int lp3943_write_byte(struct lp3943 *lp3943, u8 reg, u8 data)
91 {
92 	return regmap_write(lp3943->regmap, reg, data);
93 }
94 EXPORT_SYMBOL_GPL(lp3943_write_byte);
95 
96 int lp3943_update_bits(struct lp3943 *lp3943, u8 reg, u8 mask, u8 data)
97 {
98 	return regmap_update_bits(lp3943->regmap, reg, mask, data);
99 }
100 EXPORT_SYMBOL_GPL(lp3943_update_bits);
101 
102 static const struct regmap_config lp3943_regmap_config = {
103 	.reg_bits = 8,
104 	.val_bits = 8,
105 	.max_register = LP3943_MAX_REGISTERS,
106 };
107 
108 static int lp3943_probe(struct i2c_client *cl, const struct i2c_device_id *id)
109 {
110 	struct lp3943 *lp3943;
111 	struct device *dev = &cl->dev;
112 
113 	lp3943 = devm_kzalloc(dev, sizeof(*lp3943), GFP_KERNEL);
114 	if (!lp3943)
115 		return -ENOMEM;
116 
117 	lp3943->regmap = devm_regmap_init_i2c(cl, &lp3943_regmap_config);
118 	if (IS_ERR(lp3943->regmap))
119 		return PTR_ERR(lp3943->regmap);
120 
121 	lp3943->pdata = dev_get_platdata(dev);
122 	lp3943->dev = dev;
123 	lp3943->mux_cfg = lp3943_mux_cfg;
124 	i2c_set_clientdata(cl, lp3943);
125 
126 	return mfd_add_devices(dev, -1, lp3943_devs, ARRAY_SIZE(lp3943_devs),
127 			       NULL, 0, NULL);
128 }
129 
130 static int lp3943_remove(struct i2c_client *cl)
131 {
132 	struct lp3943 *lp3943 = i2c_get_clientdata(cl);
133 
134 	mfd_remove_devices(lp3943->dev);
135 	return 0;
136 }
137 
138 static const struct i2c_device_id lp3943_ids[] = {
139 	{ "lp3943", 0 },
140 	{ }
141 };
142 MODULE_DEVICE_TABLE(i2c, lp3943_ids);
143 
144 #ifdef CONFIG_OF
145 static const struct of_device_id lp3943_of_match[] = {
146 	{ .compatible = "ti,lp3943", },
147 	{ }
148 };
149 MODULE_DEVICE_TABLE(of, lp3943_of_match);
150 #endif
151 
152 static struct i2c_driver lp3943_driver = {
153 	.probe = lp3943_probe,
154 	.remove = lp3943_remove,
155 	.driver = {
156 		.name = "lp3943",
157 		.of_match_table = of_match_ptr(lp3943_of_match),
158 	},
159 	.id_table = lp3943_ids,
160 };
161 
162 module_i2c_driver(lp3943_driver);
163 
164 MODULE_DESCRIPTION("LP3943 MFD Core Driver");
165 MODULE_AUTHOR("Milo Kim");
166 MODULE_LICENSE("GPL");
167