Linux 5.1.15
[linux/fpc-iii.git] / drivers / mfd / lm3533-ctrlbank.c
bloba4cb7a5220a7dc4922dc990a1af7f2c973332e11
1 /*
2 * lm3533-ctrlbank.c -- LM3533 Generic Control Bank interface
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/device.h>
15 #include <linux/module.h>
17 #include <linux/mfd/lm3533.h>
20 #define LM3533_MAX_CURRENT_MIN 5000
21 #define LM3533_MAX_CURRENT_MAX 29800
22 #define LM3533_MAX_CURRENT_STEP 800
24 #define LM3533_BRIGHTNESS_MAX 255
25 #define LM3533_PWM_MAX 0x3f
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
33 static inline u8 lm3533_ctrlbank_get_reg(struct lm3533_ctrlbank *cb, u8 base)
35 return base + cb->id;
38 int lm3533_ctrlbank_enable(struct lm3533_ctrlbank *cb)
40 u8 mask;
41 int ret;
43 dev_dbg(cb->dev, "%s - %d\n", __func__, cb->id);
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);
51 return ret;
53 EXPORT_SYMBOL_GPL(lm3533_ctrlbank_enable);
55 int lm3533_ctrlbank_disable(struct lm3533_ctrlbank *cb)
57 u8 mask;
58 int ret;
60 dev_dbg(cb->dev, "%s - %d\n", __func__, cb->id);
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);
67 return ret;
69 EXPORT_SYMBOL_GPL(lm3533_ctrlbank_disable);
72 * Full-scale current.
74 * imax 5000 - 29800 uA (800 uA step)
76 int lm3533_ctrlbank_set_max_current(struct lm3533_ctrlbank *cb, u16 imax)
78 u8 reg;
79 u8 val;
80 int ret;
82 if (imax < LM3533_MAX_CURRENT_MIN || imax > LM3533_MAX_CURRENT_MAX)
83 return -EINVAL;
85 val = (imax - LM3533_MAX_CURRENT_MIN) / LM3533_MAX_CURRENT_STEP;
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");
92 return ret;
94 EXPORT_SYMBOL_GPL(lm3533_ctrlbank_set_max_current);
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; \
102 if (val > LM3533_##_NAME##_MAX) \
103 return -EINVAL; \
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"); \
110 return ret; \
112 EXPORT_SYMBOL_GPL(lm3533_ctrlbank_set_##_name);
114 #define lm3533_ctrlbank_get(_name, _NAME) \
115 int lm3533_ctrlbank_get_##_name(struct lm3533_ctrlbank *cb, u8 *val) \
117 u8 reg; \
118 int ret; \
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"); \
125 return ret; \
127 EXPORT_SYMBOL_GPL(lm3533_ctrlbank_get_##_name);
129 lm3533_ctrlbank_set(brightness, BRIGHTNESS);
130 lm3533_ctrlbank_get(brightness, BRIGHTNESS);
133 * PWM-input control mask:
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
142 lm3533_ctrlbank_set(pwm, PWM);
143 lm3533_ctrlbank_get(pwm, PWM);
146 MODULE_AUTHOR("Johan Hovold <jhovold@gmail.com>");
147 MODULE_DESCRIPTION("LM3533 Control Bank interface");
148 MODULE_LICENSE("GPL");