Thomas Gleixner | 97fb5e8 | 2019-05-29 07:17:58 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 2 | /* Copyright (c) 2015, Sony Mobile Communications, AB. |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 3 | */ |
| 4 | |
| 5 | #include <linux/kernel.h> |
Bjorn Andersson | 7ddbc24 | 2015-07-21 17:44:49 -0700 | [diff] [blame] | 6 | #include <linux/backlight.h> |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 7 | #include <linux/module.h> |
| 8 | #include <linux/of.h> |
| 9 | #include <linux/of_device.h> |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 10 | #include <linux/of_address.h> |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 11 | #include <linux/regmap.h> |
| 12 | |
Bjorn Andersson | 9d6c243 | 2015-10-26 10:45:08 -0700 | [diff] [blame] | 13 | /* From DT binding */ |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 14 | #define WLED_MAX_STRINGS 4 |
| 15 | |
Kiran Gunda | bb800a3 | 2019-11-01 11:57:00 +0530 | [diff] [blame] | 16 | #define WLED_DEFAULT_BRIGHTNESS 2048 |
Bjorn Andersson | 9d6c243 | 2015-10-26 10:45:08 -0700 | [diff] [blame] | 17 | |
Kiran Gunda | bb800a3 | 2019-11-01 11:57:00 +0530 | [diff] [blame] | 18 | #define WLED3_SINK_REG_BRIGHT_MAX 0xFFF |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 19 | |
Kiran Gunda | bb800a3 | 2019-11-01 11:57:00 +0530 | [diff] [blame] | 20 | /* WLED3 control registers */ |
| 21 | #define WLED3_CTRL_REG_MOD_EN 0x46 |
Kiran Gunda | bb800a3 | 2019-11-01 11:57:00 +0530 | [diff] [blame] | 22 | #define WLED3_CTRL_REG_MOD_EN_MASK BIT(7) |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 23 | #define WLED3_CTRL_REG_MOD_EN_SHIFT 7 |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 24 | |
Kiran Gunda | bb800a3 | 2019-11-01 11:57:00 +0530 | [diff] [blame] | 25 | #define WLED3_CTRL_REG_FREQ 0x4c |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 26 | #define WLED3_CTRL_REG_FREQ_MASK GENMASK(3, 0) |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 27 | |
Kiran Gunda | bb800a3 | 2019-11-01 11:57:00 +0530 | [diff] [blame] | 28 | #define WLED3_CTRL_REG_OVP 0x4d |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 29 | #define WLED3_CTRL_REG_OVP_MASK GENMASK(1, 0) |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 30 | |
Kiran Gunda | bb800a3 | 2019-11-01 11:57:00 +0530 | [diff] [blame] | 31 | #define WLED3_CTRL_REG_ILIMIT 0x4e |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 32 | #define WLED3_CTRL_REG_ILIMIT_MASK GENMASK(2, 0) |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 33 | |
Kiran Gunda | bb800a3 | 2019-11-01 11:57:00 +0530 | [diff] [blame] | 34 | /* WLED3 sink registers */ |
| 35 | #define WLED3_SINK_REG_SYNC 0x47 |
Kiran Gunda | bb800a3 | 2019-11-01 11:57:00 +0530 | [diff] [blame] | 36 | #define WLED3_SINK_REG_SYNC_CLEAR 0x00 |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 37 | |
Kiran Gunda | bb800a3 | 2019-11-01 11:57:00 +0530 | [diff] [blame] | 38 | #define WLED3_SINK_REG_CURR_SINK 0x4f |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 39 | #define WLED3_SINK_REG_CURR_SINK_MASK GENMASK(7, 5) |
| 40 | #define WLED3_SINK_REG_CURR_SINK_SHFT 5 |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 41 | |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 42 | /* WLED3 specific per-'string' registers below */ |
| 43 | #define WLED3_SINK_REG_BRIGHT(n) (0x40 + n) |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 44 | |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 45 | #define WLED3_SINK_REG_STR_MOD_EN(n) (0x60 + (n * 0x10)) |
Kiran Gunda | bb800a3 | 2019-11-01 11:57:00 +0530 | [diff] [blame] | 46 | #define WLED3_SINK_REG_STR_MOD_MASK BIT(7) |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 47 | |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 48 | #define WLED3_SINK_REG_STR_FULL_SCALE_CURR(n) (0x62 + (n * 0x10)) |
| 49 | #define WLED3_SINK_REG_STR_FULL_SCALE_CURR_MASK GENMASK(4, 0) |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 50 | |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 51 | #define WLED3_SINK_REG_STR_MOD_SRC(n) (0x63 + (n * 0x10)) |
| 52 | #define WLED3_SINK_REG_STR_MOD_SRC_MASK BIT(0) |
Kiran Gunda | bb800a3 | 2019-11-01 11:57:00 +0530 | [diff] [blame] | 53 | #define WLED3_SINK_REG_STR_MOD_SRC_INT 0x00 |
| 54 | #define WLED3_SINK_REG_STR_MOD_SRC_EXT 0x01 |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 55 | |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 56 | #define WLED3_SINK_REG_STR_CABC(n) (0x66 + (n * 0x10)) |
Kiran Gunda | bb800a3 | 2019-11-01 11:57:00 +0530 | [diff] [blame] | 57 | #define WLED3_SINK_REG_STR_CABC_MASK BIT(7) |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 58 | |
| 59 | struct wled_var_cfg { |
| 60 | const u32 *values; |
| 61 | u32 (*fn)(u32); |
| 62 | int size; |
| 63 | }; |
| 64 | |
| 65 | struct wled_u32_opts { |
| 66 | const char *name; |
| 67 | u32 *val_ptr; |
| 68 | const struct wled_var_cfg *cfg; |
| 69 | }; |
| 70 | |
| 71 | struct wled_bool_opts { |
| 72 | const char *name; |
| 73 | bool *val_ptr; |
| 74 | }; |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 75 | |
Kiran Gunda | bb800a3 | 2019-11-01 11:57:00 +0530 | [diff] [blame] | 76 | struct wled_config { |
| 77 | u32 boost_i_limit; |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 78 | u32 ovp; |
| 79 | u32 switch_freq; |
| 80 | u32 num_strings; |
Kiran Gunda | bb800a3 | 2019-11-01 11:57:00 +0530 | [diff] [blame] | 81 | u32 string_i_limit; |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 82 | u32 enabled_strings[WLED_MAX_STRINGS]; |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 83 | bool cs_out_en; |
| 84 | bool ext_gen; |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 85 | bool cabc; |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 86 | }; |
| 87 | |
Kiran Gunda | bb800a3 | 2019-11-01 11:57:00 +0530 | [diff] [blame] | 88 | struct wled { |
Bjorn Andersson | 7ddbc24 | 2015-07-21 17:44:49 -0700 | [diff] [blame] | 89 | const char *name; |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 90 | struct device *dev; |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 91 | struct regmap *regmap; |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 92 | u16 ctrl_addr; |
| 93 | u16 max_string_count; |
| 94 | u32 brightness; |
| 95 | u32 max_brightness; |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 96 | |
Kiran Gunda | bb800a3 | 2019-11-01 11:57:00 +0530 | [diff] [blame] | 97 | struct wled_config cfg; |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 98 | int (*wled_set_brightness)(struct wled *wled, u16 brightness); |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 101 | static int wled3_set_brightness(struct wled *wled, u16 brightness) |
| 102 | { |
| 103 | int rc, i; |
| 104 | u8 v[2]; |
| 105 | |
| 106 | v[0] = brightness & 0xff; |
| 107 | v[1] = (brightness >> 8) & 0xf; |
| 108 | |
| 109 | for (i = 0; i < wled->cfg.num_strings; ++i) { |
| 110 | rc = regmap_bulk_write(wled->regmap, wled->ctrl_addr + |
| 111 | WLED3_SINK_REG_BRIGHT(i), v, 2); |
| 112 | if (rc < 0) |
| 113 | return rc; |
| 114 | } |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | static int wled_module_enable(struct wled *wled, int val) |
| 120 | { |
| 121 | int rc; |
| 122 | |
| 123 | rc = regmap_update_bits(wled->regmap, wled->ctrl_addr + |
| 124 | WLED3_CTRL_REG_MOD_EN, |
| 125 | WLED3_CTRL_REG_MOD_EN_MASK, |
| 126 | val << WLED3_CTRL_REG_MOD_EN_SHIFT); |
| 127 | return rc; |
| 128 | } |
| 129 | |
| 130 | static int wled_sync_toggle(struct wled *wled) |
| 131 | { |
| 132 | int rc; |
| 133 | unsigned int mask = GENMASK(wled->max_string_count - 1, 0); |
| 134 | |
| 135 | rc = regmap_update_bits(wled->regmap, |
| 136 | wled->ctrl_addr + WLED3_SINK_REG_SYNC, |
| 137 | mask, mask); |
| 138 | if (rc < 0) |
| 139 | return rc; |
| 140 | |
| 141 | rc = regmap_update_bits(wled->regmap, |
| 142 | wled->ctrl_addr + WLED3_SINK_REG_SYNC, |
| 143 | mask, WLED3_SINK_REG_SYNC_CLEAR); |
| 144 | |
| 145 | return rc; |
| 146 | } |
| 147 | |
Kiran Gunda | bb800a3 | 2019-11-01 11:57:00 +0530 | [diff] [blame] | 148 | static int wled_update_status(struct backlight_device *bl) |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 149 | { |
Kiran Gunda | bb800a3 | 2019-11-01 11:57:00 +0530 | [diff] [blame] | 150 | struct wled *wled = bl_get_data(bl); |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 151 | u16 brightness = bl->props.brightness; |
| 152 | int rc = 0; |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 153 | |
Bjorn Andersson | 7ddbc24 | 2015-07-21 17:44:49 -0700 | [diff] [blame] | 154 | if (bl->props.power != FB_BLANK_UNBLANK || |
| 155 | bl->props.fb_blank != FB_BLANK_UNBLANK || |
| 156 | bl->props.state & BL_CORE_FBBLANK) |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 157 | brightness = 0; |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 158 | |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 159 | if (brightness) { |
| 160 | rc = wled->wled_set_brightness(wled, brightness); |
| 161 | if (rc < 0) { |
| 162 | dev_err(wled->dev, "wled failed to set brightness rc:%d\n", |
| 163 | rc); |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 164 | return rc; |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 165 | } |
| 166 | |
| 167 | rc = wled_sync_toggle(wled); |
| 168 | if (rc < 0) { |
| 169 | dev_err(wled->dev, "wled sync failed rc:%d\n", rc); |
| 170 | return rc; |
| 171 | } |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 174 | if (!!brightness != !!wled->brightness) { |
| 175 | rc = wled_module_enable(wled, !!brightness); |
| 176 | if (rc < 0) { |
| 177 | dev_err(wled->dev, "wled enable failed rc:%d\n", rc); |
| 178 | return rc; |
| 179 | } |
| 180 | } |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 181 | |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 182 | wled->brightness = brightness; |
| 183 | |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 184 | return rc; |
| 185 | } |
| 186 | |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 187 | static int wled3_setup(struct wled *wled) |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 188 | { |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 189 | u16 addr; |
| 190 | u8 sink_en = 0; |
| 191 | int rc, i, j; |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 192 | |
| 193 | rc = regmap_update_bits(wled->regmap, |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 194 | wled->ctrl_addr + WLED3_CTRL_REG_OVP, |
| 195 | WLED3_CTRL_REG_OVP_MASK, wled->cfg.ovp); |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 196 | if (rc) |
| 197 | return rc; |
| 198 | |
| 199 | rc = regmap_update_bits(wled->regmap, |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 200 | wled->ctrl_addr + WLED3_CTRL_REG_ILIMIT, |
| 201 | WLED3_CTRL_REG_ILIMIT_MASK, |
| 202 | wled->cfg.boost_i_limit); |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 203 | if (rc) |
| 204 | return rc; |
| 205 | |
| 206 | rc = regmap_update_bits(wled->regmap, |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 207 | wled->ctrl_addr + WLED3_CTRL_REG_FREQ, |
| 208 | WLED3_CTRL_REG_FREQ_MASK, |
| 209 | wled->cfg.switch_freq); |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 210 | if (rc) |
| 211 | return rc; |
| 212 | |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 213 | for (i = 0; i < wled->cfg.num_strings; ++i) { |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 214 | j = wled->cfg.enabled_strings[i]; |
| 215 | addr = wled->ctrl_addr + WLED3_SINK_REG_STR_MOD_EN(j); |
| 216 | rc = regmap_update_bits(wled->regmap, addr, |
| 217 | WLED3_SINK_REG_STR_MOD_MASK, |
| 218 | WLED3_SINK_REG_STR_MOD_MASK); |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 219 | if (rc) |
| 220 | return rc; |
| 221 | |
| 222 | if (wled->cfg.ext_gen) { |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 223 | addr = wled->ctrl_addr + WLED3_SINK_REG_STR_MOD_SRC(j); |
| 224 | rc = regmap_update_bits(wled->regmap, addr, |
| 225 | WLED3_SINK_REG_STR_MOD_SRC_MASK, |
| 226 | WLED3_SINK_REG_STR_MOD_SRC_EXT); |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 227 | if (rc) |
| 228 | return rc; |
| 229 | } |
| 230 | |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 231 | addr = wled->ctrl_addr + WLED3_SINK_REG_STR_FULL_SCALE_CURR(j); |
| 232 | rc = regmap_update_bits(wled->regmap, addr, |
| 233 | WLED3_SINK_REG_STR_FULL_SCALE_CURR_MASK, |
| 234 | wled->cfg.string_i_limit); |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 235 | if (rc) |
| 236 | return rc; |
| 237 | |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 238 | addr = wled->ctrl_addr + WLED3_SINK_REG_STR_CABC(j); |
| 239 | rc = regmap_update_bits(wled->regmap, addr, |
| 240 | WLED3_SINK_REG_STR_CABC_MASK, |
| 241 | wled->cfg.cabc ? |
| 242 | WLED3_SINK_REG_STR_CABC_MASK : 0); |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 243 | if (rc) |
| 244 | return rc; |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 245 | |
| 246 | sink_en |= BIT(j + WLED3_SINK_REG_CURR_SINK_SHFT); |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 249 | rc = regmap_update_bits(wled->regmap, |
| 250 | wled->ctrl_addr + WLED3_SINK_REG_CURR_SINK, |
| 251 | WLED3_SINK_REG_CURR_SINK_MASK, sink_en); |
| 252 | if (rc) |
| 253 | return rc; |
| 254 | |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 255 | return 0; |
| 256 | } |
| 257 | |
Kiran Gunda | bb800a3 | 2019-11-01 11:57:00 +0530 | [diff] [blame] | 258 | static const struct wled_config wled3_config_defaults = { |
| 259 | .boost_i_limit = 3, |
| 260 | .string_i_limit = 20, |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 261 | .ovp = 2, |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 262 | .num_strings = 3, |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 263 | .switch_freq = 5, |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 264 | .cs_out_en = false, |
| 265 | .ext_gen = false, |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 266 | .cabc = false, |
| 267 | .enabled_strings = {0, 1, 2, 3}, |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 268 | }; |
| 269 | |
Kiran Gunda | bb800a3 | 2019-11-01 11:57:00 +0530 | [diff] [blame] | 270 | static const u32 wled3_boost_i_limit_values[] = { |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 271 | 105, 385, 525, 805, 980, 1260, 1400, 1680, |
| 272 | }; |
| 273 | |
Kiran Gunda | bb800a3 | 2019-11-01 11:57:00 +0530 | [diff] [blame] | 274 | static const struct wled_var_cfg wled3_boost_i_limit_cfg = { |
| 275 | .values = wled3_boost_i_limit_values, |
| 276 | .size = ARRAY_SIZE(wled3_boost_i_limit_values), |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 277 | }; |
| 278 | |
Kiran Gunda | bb800a3 | 2019-11-01 11:57:00 +0530 | [diff] [blame] | 279 | static const u32 wled3_ovp_values[] = { |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 280 | 35, 32, 29, 27, |
| 281 | }; |
| 282 | |
Kiran Gunda | bb800a3 | 2019-11-01 11:57:00 +0530 | [diff] [blame] | 283 | static const struct wled_var_cfg wled3_ovp_cfg = { |
| 284 | .values = wled3_ovp_values, |
| 285 | .size = ARRAY_SIZE(wled3_ovp_values), |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 286 | }; |
| 287 | |
Kiran Gunda | bb800a3 | 2019-11-01 11:57:00 +0530 | [diff] [blame] | 288 | static u32 wled3_num_strings_values_fn(u32 idx) |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 289 | { |
| 290 | return idx + 1; |
| 291 | } |
| 292 | |
Kiran Gunda | bb800a3 | 2019-11-01 11:57:00 +0530 | [diff] [blame] | 293 | static const struct wled_var_cfg wled3_num_strings_cfg = { |
| 294 | .fn = wled3_num_strings_values_fn, |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 295 | .size = 3, |
| 296 | }; |
| 297 | |
Kiran Gunda | bb800a3 | 2019-11-01 11:57:00 +0530 | [diff] [blame] | 298 | static u32 wled3_switch_freq_values_fn(u32 idx) |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 299 | { |
| 300 | return 19200 / (2 * (1 + idx)); |
| 301 | } |
| 302 | |
Kiran Gunda | bb800a3 | 2019-11-01 11:57:00 +0530 | [diff] [blame] | 303 | static const struct wled_var_cfg wled3_switch_freq_cfg = { |
| 304 | .fn = wled3_switch_freq_values_fn, |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 305 | .size = 16, |
| 306 | }; |
| 307 | |
Kiran Gunda | bb800a3 | 2019-11-01 11:57:00 +0530 | [diff] [blame] | 308 | static const struct wled_var_cfg wled3_string_i_limit_cfg = { |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 309 | .size = 26, |
| 310 | }; |
| 311 | |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 312 | static const struct wled_var_cfg wled3_string_cfg = { |
| 313 | .size = 8, |
| 314 | }; |
| 315 | |
| 316 | static u32 wled_values(const struct wled_var_cfg *cfg, u32 idx) |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 317 | { |
| 318 | if (idx >= cfg->size) |
| 319 | return UINT_MAX; |
| 320 | if (cfg->fn) |
| 321 | return cfg->fn(idx); |
| 322 | if (cfg->values) |
| 323 | return cfg->values[idx]; |
| 324 | return idx; |
| 325 | } |
| 326 | |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 327 | static int wled_configure(struct wled *wled, int version) |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 328 | { |
Kiran Gunda | bb800a3 | 2019-11-01 11:57:00 +0530 | [diff] [blame] | 329 | struct wled_config *cfg = &wled->cfg; |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 330 | struct device *dev = wled->dev; |
| 331 | const __be32 *prop_addr; |
| 332 | u32 size, val, c, string_len; |
| 333 | int rc, i, j; |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 334 | |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 335 | const struct wled_u32_opts *u32_opts = NULL; |
| 336 | const struct wled_u32_opts wled3_opts[] = { |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 337 | { |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 338 | .name = "qcom,current-boost-limit", |
| 339 | .val_ptr = &cfg->boost_i_limit, |
Kiran Gunda | bb800a3 | 2019-11-01 11:57:00 +0530 | [diff] [blame] | 340 | .cfg = &wled3_boost_i_limit_cfg, |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 341 | }, |
| 342 | { |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 343 | .name = "qcom,current-limit", |
| 344 | .val_ptr = &cfg->string_i_limit, |
Kiran Gunda | bb800a3 | 2019-11-01 11:57:00 +0530 | [diff] [blame] | 345 | .cfg = &wled3_string_i_limit_cfg, |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 346 | }, |
| 347 | { |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 348 | .name = "qcom,ovp", |
| 349 | .val_ptr = &cfg->ovp, |
Kiran Gunda | bb800a3 | 2019-11-01 11:57:00 +0530 | [diff] [blame] | 350 | .cfg = &wled3_ovp_cfg, |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 351 | }, |
| 352 | { |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 353 | .name = "qcom,switching-freq", |
| 354 | .val_ptr = &cfg->switch_freq, |
Kiran Gunda | bb800a3 | 2019-11-01 11:57:00 +0530 | [diff] [blame] | 355 | .cfg = &wled3_switch_freq_cfg, |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 356 | }, |
| 357 | { |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 358 | .name = "qcom,num-strings", |
| 359 | .val_ptr = &cfg->num_strings, |
Kiran Gunda | bb800a3 | 2019-11-01 11:57:00 +0530 | [diff] [blame] | 360 | .cfg = &wled3_num_strings_cfg, |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 361 | }, |
| 362 | }; |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 363 | |
| 364 | const struct wled_bool_opts bool_opts[] = { |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 365 | { "qcom,cs-out", &cfg->cs_out_en, }, |
| 366 | { "qcom,ext-gen", &cfg->ext_gen, }, |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 367 | { "qcom,cabc", &cfg->cabc, }, |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 368 | }; |
| 369 | |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 370 | prop_addr = of_get_address(dev->of_node, 0, NULL, NULL); |
| 371 | if (!prop_addr) { |
| 372 | dev_err(wled->dev, "invalid IO resources\n"); |
| 373 | return -EINVAL; |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 374 | } |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 375 | wled->ctrl_addr = be32_to_cpu(*prop_addr); |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 376 | |
Bjorn Andersson | 7ddbc24 | 2015-07-21 17:44:49 -0700 | [diff] [blame] | 377 | rc = of_property_read_string(dev->of_node, "label", &wled->name); |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 378 | if (rc) |
Rob Herring | f86b775 | 2018-08-27 20:03:42 -0500 | [diff] [blame] | 379 | wled->name = devm_kasprintf(dev, GFP_KERNEL, "%pOFn", dev->of_node); |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 380 | |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 381 | switch (version) { |
| 382 | case 3: |
| 383 | u32_opts = wled3_opts; |
| 384 | size = ARRAY_SIZE(wled3_opts); |
| 385 | *cfg = wled3_config_defaults; |
| 386 | wled->wled_set_brightness = wled3_set_brightness; |
| 387 | wled->max_string_count = 3; |
| 388 | break; |
| 389 | |
| 390 | default: |
| 391 | dev_err(wled->dev, "Invalid WLED version\n"); |
| 392 | return -EINVAL; |
| 393 | } |
| 394 | |
| 395 | for (i = 0; i < size; ++i) { |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 396 | rc = of_property_read_u32(dev->of_node, u32_opts[i].name, &val); |
| 397 | if (rc == -EINVAL) { |
| 398 | continue; |
| 399 | } else if (rc) { |
| 400 | dev_err(dev, "error reading '%s'\n", u32_opts[i].name); |
| 401 | return rc; |
| 402 | } |
| 403 | |
| 404 | c = UINT_MAX; |
| 405 | for (j = 0; c != val; j++) { |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 406 | c = wled_values(u32_opts[i].cfg, j); |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 407 | if (c == UINT_MAX) { |
| 408 | dev_err(dev, "invalid value for '%s'\n", |
| 409 | u32_opts[i].name); |
| 410 | return -EINVAL; |
| 411 | } |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 412 | |
| 413 | if (c == val) |
| 414 | break; |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | dev_dbg(dev, "'%s' = %u\n", u32_opts[i].name, c); |
| 418 | *u32_opts[i].val_ptr = j; |
| 419 | } |
| 420 | |
| 421 | for (i = 0; i < ARRAY_SIZE(bool_opts); ++i) { |
| 422 | if (of_property_read_bool(dev->of_node, bool_opts[i].name)) |
| 423 | *bool_opts[i].val_ptr = true; |
| 424 | } |
| 425 | |
| 426 | cfg->num_strings = cfg->num_strings + 1; |
| 427 | |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 428 | string_len = of_property_count_elems_of_size(dev->of_node, |
| 429 | "qcom,enabled-strings", |
| 430 | sizeof(u32)); |
| 431 | if (string_len > 0) |
| 432 | of_property_read_u32_array(dev->of_node, |
| 433 | "qcom,enabled-strings", |
| 434 | wled->cfg.enabled_strings, |
| 435 | sizeof(u32)); |
| 436 | |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 437 | return 0; |
| 438 | } |
| 439 | |
Kiran Gunda | bb800a3 | 2019-11-01 11:57:00 +0530 | [diff] [blame] | 440 | static const struct backlight_ops wled_ops = { |
| 441 | .update_status = wled_update_status, |
Bjorn Andersson | 7ddbc24 | 2015-07-21 17:44:49 -0700 | [diff] [blame] | 442 | }; |
| 443 | |
Kiran Gunda | bb800a3 | 2019-11-01 11:57:00 +0530 | [diff] [blame] | 444 | static int wled_probe(struct platform_device *pdev) |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 445 | { |
Bjorn Andersson | 7ddbc24 | 2015-07-21 17:44:49 -0700 | [diff] [blame] | 446 | struct backlight_properties props; |
| 447 | struct backlight_device *bl; |
Kiran Gunda | bb800a3 | 2019-11-01 11:57:00 +0530 | [diff] [blame] | 448 | struct wled *wled; |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 449 | struct regmap *regmap; |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 450 | int version; |
Bjorn Andersson | 9d6c243 | 2015-10-26 10:45:08 -0700 | [diff] [blame] | 451 | u32 val; |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 452 | int rc; |
| 453 | |
| 454 | regmap = dev_get_regmap(pdev->dev.parent, NULL); |
| 455 | if (!regmap) { |
| 456 | dev_err(&pdev->dev, "Unable to get regmap\n"); |
| 457 | return -EINVAL; |
| 458 | } |
| 459 | |
| 460 | wled = devm_kzalloc(&pdev->dev, sizeof(*wled), GFP_KERNEL); |
| 461 | if (!wled) |
| 462 | return -ENOMEM; |
| 463 | |
| 464 | wled->regmap = regmap; |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 465 | wled->dev = &pdev->dev; |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 466 | |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 467 | version = (uintptr_t)of_device_get_match_data(&pdev->dev); |
| 468 | if (!version) { |
| 469 | dev_err(&pdev->dev, "Unknown device version\n"); |
| 470 | return -ENODEV; |
| 471 | } |
| 472 | |
| 473 | rc = wled_configure(wled, version); |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 474 | if (rc) |
| 475 | return rc; |
| 476 | |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 477 | switch (version) { |
| 478 | case 3: |
| 479 | rc = wled3_setup(wled); |
| 480 | if (rc) { |
| 481 | dev_err(&pdev->dev, "wled3_setup failed\n"); |
| 482 | return rc; |
| 483 | } |
| 484 | break; |
| 485 | |
| 486 | default: |
| 487 | dev_err(wled->dev, "Invalid WLED version\n"); |
| 488 | break; |
| 489 | } |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 490 | |
Kiran Gunda | bb800a3 | 2019-11-01 11:57:00 +0530 | [diff] [blame] | 491 | val = WLED_DEFAULT_BRIGHTNESS; |
Bjorn Andersson | 9d6c243 | 2015-10-26 10:45:08 -0700 | [diff] [blame] | 492 | of_property_read_u32(pdev->dev.of_node, "default-brightness", &val); |
| 493 | |
Bjorn Andersson | 7ddbc24 | 2015-07-21 17:44:49 -0700 | [diff] [blame] | 494 | memset(&props, 0, sizeof(struct backlight_properties)); |
| 495 | props.type = BACKLIGHT_RAW; |
Bjorn Andersson | 9d6c243 | 2015-10-26 10:45:08 -0700 | [diff] [blame] | 496 | props.brightness = val; |
Kiran Gunda | bb800a3 | 2019-11-01 11:57:00 +0530 | [diff] [blame] | 497 | props.max_brightness = WLED3_SINK_REG_BRIGHT_MAX; |
Bjorn Andersson | 7ddbc24 | 2015-07-21 17:44:49 -0700 | [diff] [blame] | 498 | bl = devm_backlight_device_register(&pdev->dev, wled->name, |
| 499 | &pdev->dev, wled, |
Kiran Gunda | bb800a3 | 2019-11-01 11:57:00 +0530 | [diff] [blame] | 500 | &wled_ops, &props); |
kbuild test robot | fc18111 | 2015-10-27 02:26:11 +0800 | [diff] [blame] | 501 | return PTR_ERR_OR_ZERO(bl); |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 502 | }; |
| 503 | |
Kiran Gunda | bb800a3 | 2019-11-01 11:57:00 +0530 | [diff] [blame] | 504 | static const struct of_device_id wled_match_table[] = { |
Kiran Gunda | 775d2ff | 2019-11-01 11:57:01 +0530 | [diff] [blame^] | 505 | { .compatible = "qcom,pm8941-wled", .data = (void *)3 }, |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 506 | {} |
| 507 | }; |
Kiran Gunda | bb800a3 | 2019-11-01 11:57:00 +0530 | [diff] [blame] | 508 | MODULE_DEVICE_TABLE(of, wled_match_table); |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 509 | |
Kiran Gunda | bb800a3 | 2019-11-01 11:57:00 +0530 | [diff] [blame] | 510 | static struct platform_driver wled_driver = { |
| 511 | .probe = wled_probe, |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 512 | .driver = { |
Kiran Gunda | bb800a3 | 2019-11-01 11:57:00 +0530 | [diff] [blame] | 513 | .name = "qcom,wled", |
| 514 | .of_match_table = wled_match_table, |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 515 | }, |
| 516 | }; |
| 517 | |
Kiran Gunda | bb800a3 | 2019-11-01 11:57:00 +0530 | [diff] [blame] | 518 | module_platform_driver(wled_driver); |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 519 | |
Kiran Gunda | bb800a3 | 2019-11-01 11:57:00 +0530 | [diff] [blame] | 520 | MODULE_DESCRIPTION("Qualcomm WLED driver"); |
Courtney Cavin | 93c64f1 | 2015-03-12 08:47:08 -0700 | [diff] [blame] | 521 | MODULE_LICENSE("GPL v2"); |