treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / mfd / lm3533-ctrlbank.c
blob34fba06ec7057fb0f1033477b1be21f105721a16
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * lm3533-ctrlbank.c -- LM3533 Generic Control Bank interface
5 * Copyright (C) 2011-2012 Texas Instruments
7 * Author: Johan Hovold <jhovold@gmail.com>
8 */
10 #include <linux/device.h>
11 #include <linux/module.h>
13 #include <linux/mfd/lm3533.h>
16 #define LM3533_MAX_CURRENT_MIN 5000
17 #define LM3533_MAX_CURRENT_MAX 29800
18 #define LM3533_MAX_CURRENT_STEP 800
20 #define LM3533_BRIGHTNESS_MAX 255
21 #define LM3533_PWM_MAX 0x3f
23 #define LM3533_REG_PWM_BASE 0x14
24 #define LM3533_REG_MAX_CURRENT_BASE 0x1f
25 #define LM3533_REG_CTRLBANK_ENABLE 0x27
26 #define LM3533_REG_BRIGHTNESS_BASE 0x40
29 static inline u8 lm3533_ctrlbank_get_reg(struct lm3533_ctrlbank *cb, u8 base)
31 return base + cb->id;
34 int lm3533_ctrlbank_enable(struct lm3533_ctrlbank *cb)
36 u8 mask;
37 int ret;
39 dev_dbg(cb->dev, "%s - %d\n", __func__, cb->id);
41 mask = 1 << cb->id;
42 ret = lm3533_update(cb->lm3533, LM3533_REG_CTRLBANK_ENABLE,
43 mask, mask);
44 if (ret)
45 dev_err(cb->dev, "failed to enable ctrlbank %d\n", cb->id);
47 return ret;
49 EXPORT_SYMBOL_GPL(lm3533_ctrlbank_enable);
51 int lm3533_ctrlbank_disable(struct lm3533_ctrlbank *cb)
53 u8 mask;
54 int ret;
56 dev_dbg(cb->dev, "%s - %d\n", __func__, cb->id);
58 mask = 1 << cb->id;
59 ret = lm3533_update(cb->lm3533, LM3533_REG_CTRLBANK_ENABLE, 0, mask);
60 if (ret)
61 dev_err(cb->dev, "failed to disable ctrlbank %d\n", cb->id);
63 return ret;
65 EXPORT_SYMBOL_GPL(lm3533_ctrlbank_disable);
68 * Full-scale current.
70 * imax 5000 - 29800 uA (800 uA step)
72 int lm3533_ctrlbank_set_max_current(struct lm3533_ctrlbank *cb, u16 imax)
74 u8 reg;
75 u8 val;
76 int ret;
78 if (imax < LM3533_MAX_CURRENT_MIN || imax > LM3533_MAX_CURRENT_MAX)
79 return -EINVAL;
81 val = (imax - LM3533_MAX_CURRENT_MIN) / LM3533_MAX_CURRENT_STEP;
83 reg = lm3533_ctrlbank_get_reg(cb, LM3533_REG_MAX_CURRENT_BASE);
84 ret = lm3533_write(cb->lm3533, reg, val);
85 if (ret)
86 dev_err(cb->dev, "failed to set max current\n");
88 return ret;
90 EXPORT_SYMBOL_GPL(lm3533_ctrlbank_set_max_current);
92 #define lm3533_ctrlbank_set(_name, _NAME) \
93 int lm3533_ctrlbank_set_##_name(struct lm3533_ctrlbank *cb, u8 val) \
94 { \
95 u8 reg; \
96 int ret; \
98 if (val > LM3533_##_NAME##_MAX) \
99 return -EINVAL; \
101 reg = lm3533_ctrlbank_get_reg(cb, LM3533_REG_##_NAME##_BASE); \
102 ret = lm3533_write(cb->lm3533, reg, val); \
103 if (ret) \
104 dev_err(cb->dev, "failed to set " #_name "\n"); \
106 return ret; \
108 EXPORT_SYMBOL_GPL(lm3533_ctrlbank_set_##_name);
110 #define lm3533_ctrlbank_get(_name, _NAME) \
111 int lm3533_ctrlbank_get_##_name(struct lm3533_ctrlbank *cb, u8 *val) \
113 u8 reg; \
114 int ret; \
116 reg = lm3533_ctrlbank_get_reg(cb, LM3533_REG_##_NAME##_BASE); \
117 ret = lm3533_read(cb->lm3533, reg, val); \
118 if (ret) \
119 dev_err(cb->dev, "failed to get " #_name "\n"); \
121 return ret; \
123 EXPORT_SYMBOL_GPL(lm3533_ctrlbank_get_##_name);
125 lm3533_ctrlbank_set(brightness, BRIGHTNESS);
126 lm3533_ctrlbank_get(brightness, BRIGHTNESS);
129 * PWM-input control mask:
131 * bit 5 - PWM-input enabled in Zone 4
132 * bit 4 - PWM-input enabled in Zone 3
133 * bit 3 - PWM-input enabled in Zone 2
134 * bit 2 - PWM-input enabled in Zone 1
135 * bit 1 - PWM-input enabled in Zone 0
136 * bit 0 - PWM-input enabled
138 lm3533_ctrlbank_set(pwm, PWM);
139 lm3533_ctrlbank_get(pwm, PWM);
142 MODULE_AUTHOR("Johan Hovold <jhovold@gmail.com>");
143 MODULE_DESCRIPTION("LM3533 Control Bank interface");
144 MODULE_LICENSE("GPL");