xref: /openbmc/linux/drivers/mfd/lm3533-ctrlbank.c (revision e23feb16)
1 /*
2  * lm3533-ctrlbank.c -- LM3533 Generic Control Bank interface
3  *
4  * Copyright (C) 2011-2012 Texas Instruments
5  *
6  * Author: Johan Hovold <jhovold@gmail.com>
7  *
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.
12  */
13 
14 #include <linux/device.h>
15 #include <linux/module.h>
16 
17 #include <linux/mfd/lm3533.h>
18 
19 
20 #define LM3533_MAX_CURRENT_MIN		5000
21 #define LM3533_MAX_CURRENT_MAX		29800
22 #define LM3533_MAX_CURRENT_STEP		800
23 
24 #define LM3533_BRIGHTNESS_MAX		255
25 #define LM3533_PWM_MAX			0x3f
26 
27 #define LM3533_REG_PWM_BASE		0x14
28 #define LM3533_REG_MAX_CURRENT_BASE	0x1f
29 #define LM3533_REG_CTRLBANK_ENABLE	0x27
30 #define LM3533_REG_BRIGHTNESS_BASE	0x40
31 
32 
33 static inline u8 lm3533_ctrlbank_get_reg(struct lm3533_ctrlbank *cb, u8 base)
34 {
35 	return base + cb->id;
36 }
37 
38 int lm3533_ctrlbank_enable(struct lm3533_ctrlbank *cb)
39 {
40 	u8 mask;
41 	int ret;
42 
43 	dev_dbg(cb->dev, "%s - %d\n", __func__, cb->id);
44 
45 	mask = 1 << cb->id;
46 	ret = lm3533_update(cb->lm3533, LM3533_REG_CTRLBANK_ENABLE,
47 								mask, mask);
48 	if (ret)
49 		dev_err(cb->dev, "failed to enable ctrlbank %d\n", cb->id);
50 
51 	return ret;
52 }
53 EXPORT_SYMBOL_GPL(lm3533_ctrlbank_enable);
54 
55 int lm3533_ctrlbank_disable(struct lm3533_ctrlbank *cb)
56 {
57 	u8 mask;
58 	int ret;
59 
60 	dev_dbg(cb->dev, "%s - %d\n", __func__, cb->id);
61 
62 	mask = 1 << cb->id;
63 	ret = lm3533_update(cb->lm3533, LM3533_REG_CTRLBANK_ENABLE, 0, mask);
64 	if (ret)
65 		dev_err(cb->dev, "failed to disable ctrlbank %d\n", cb->id);
66 
67 	return ret;
68 }
69 EXPORT_SYMBOL_GPL(lm3533_ctrlbank_disable);
70 
71 /*
72  * Full-scale current.
73  *
74  * imax		5000 - 29800 uA (800 uA step)
75  */
76 int lm3533_ctrlbank_set_max_current(struct lm3533_ctrlbank *cb, u16 imax)
77 {
78 	u8 reg;
79 	u8 val;
80 	int ret;
81 
82 	if (imax < LM3533_MAX_CURRENT_MIN || imax > LM3533_MAX_CURRENT_MAX)
83 		return -EINVAL;
84 
85 	val = (imax - LM3533_MAX_CURRENT_MIN) / LM3533_MAX_CURRENT_STEP;
86 
87 	reg = lm3533_ctrlbank_get_reg(cb, LM3533_REG_MAX_CURRENT_BASE);
88 	ret = lm3533_write(cb->lm3533, reg, val);
89 	if (ret)
90 		dev_err(cb->dev, "failed to set max current\n");
91 
92 	return ret;
93 }
94 EXPORT_SYMBOL_GPL(lm3533_ctrlbank_set_max_current);
95 
96 #define lm3533_ctrlbank_set(_name, _NAME)				\
97 int lm3533_ctrlbank_set_##_name(struct lm3533_ctrlbank *cb, u8 val)	\
98 {									\
99 	u8 reg;								\
100 	int ret;							\
101 									\
102 	if (val > LM3533_##_NAME##_MAX)					\
103 		return -EINVAL;						\
104 									\
105 	reg = lm3533_ctrlbank_get_reg(cb, LM3533_REG_##_NAME##_BASE);	\
106 	ret = lm3533_write(cb->lm3533, reg, val);			\
107 	if (ret)							\
108 		dev_err(cb->dev, "failed to set " #_name "\n");		\
109 									\
110 	return ret;							\
111 }									\
112 EXPORT_SYMBOL_GPL(lm3533_ctrlbank_set_##_name);
113 
114 #define lm3533_ctrlbank_get(_name, _NAME)				\
115 int lm3533_ctrlbank_get_##_name(struct lm3533_ctrlbank *cb, u8 *val)	\
116 {									\
117 	u8 reg;								\
118 	int ret;							\
119 									\
120 	reg = lm3533_ctrlbank_get_reg(cb, LM3533_REG_##_NAME##_BASE);	\
121 	ret = lm3533_read(cb->lm3533, reg, val);			\
122 	if (ret)							\
123 		dev_err(cb->dev, "failed to get " #_name "\n");		\
124 									\
125 	return ret;							\
126 }									\
127 EXPORT_SYMBOL_GPL(lm3533_ctrlbank_get_##_name);
128 
129 lm3533_ctrlbank_set(brightness, BRIGHTNESS);
130 lm3533_ctrlbank_get(brightness, BRIGHTNESS);
131 
132 /*
133  * PWM-input control mask:
134  *
135  *   bit 5 - PWM-input enabled in Zone 4
136  *   bit 4 - PWM-input enabled in Zone 3
137  *   bit 3 - PWM-input enabled in Zone 2
138  *   bit 2 - PWM-input enabled in Zone 1
139  *   bit 1 - PWM-input enabled in Zone 0
140  *   bit 0 - PWM-input enabled
141  */
142 lm3533_ctrlbank_set(pwm, PWM);
143 lm3533_ctrlbank_get(pwm, PWM);
144 
145 
146 MODULE_AUTHOR("Johan Hovold <jhovold@gmail.com>");
147 MODULE_DESCRIPTION("LM3533 Control Bank interface");
148 MODULE_LICENSE("GPL");
149