| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Copyright 2011 bct electronic GmbH |
|---|
| 3 | 4 | * Copyright 2013 Qtechnology/AS |
|---|
| 4 | 5 | * |
|---|
| 5 | 6 | * Author: Peter Meerwald <p.meerwald@bct-electronic.com> |
|---|
| 6 | | - * Author: Ricardo Ribalda <ricardo.ribalda@gmail.com> |
|---|
| 7 | + * Author: Ricardo Ribalda <ribalda@kernel.org> |
|---|
| 7 | 8 | * |
|---|
| 8 | 9 | * Based on leds-pca955x.c |
|---|
| 9 | | - * |
|---|
| 10 | | - * This file is subject to the terms and conditions of version 2 of |
|---|
| 11 | | - * the GNU General Public License. See the file COPYING in the main |
|---|
| 12 | | - * directory of this archive for more details. |
|---|
| 13 | 10 | * |
|---|
| 14 | 11 | * LED driver for the PCA9633 I2C LED driver (7-bit slave address 0x62) |
|---|
| 15 | 12 | * LED driver for the PCA9634/5 I2C LED driver (7-bit slave address set by hw.) |
|---|
| .. | .. |
|---|
| 25 | 22 | * or by adding the 'nxp,hw-blink' property to the DTS. |
|---|
| 26 | 23 | */ |
|---|
| 27 | 24 | |
|---|
| 28 | | -#include <linux/acpi.h> |
|---|
| 29 | 25 | #include <linux/module.h> |
|---|
| 30 | 26 | #include <linux/delay.h> |
|---|
| 31 | 27 | #include <linux/string.h> |
|---|
| .. | .. |
|---|
| 33 | 29 | #include <linux/leds.h> |
|---|
| 34 | 30 | #include <linux/err.h> |
|---|
| 35 | 31 | #include <linux/i2c.h> |
|---|
| 32 | +#include <linux/property.h> |
|---|
| 36 | 33 | #include <linux/slab.h> |
|---|
| 37 | 34 | #include <linux/of.h> |
|---|
| 38 | | -#include <linux/platform_data/leds-pca963x.h> |
|---|
| 39 | 35 | |
|---|
| 40 | 36 | /* LED select registers determine the source that drives LED outputs */ |
|---|
| 41 | 37 | #define PCA963X_LED_OFF 0x0 /* LED driver off */ |
|---|
| .. | .. |
|---|
| 99 | 95 | }; |
|---|
| 100 | 96 | MODULE_DEVICE_TABLE(i2c, pca963x_id); |
|---|
| 101 | 97 | |
|---|
| 102 | | -static const struct acpi_device_id pca963x_acpi_ids[] = { |
|---|
| 103 | | - { "PCA9632", pca9633 }, |
|---|
| 104 | | - { "PCA9633", pca9633 }, |
|---|
| 105 | | - { "PCA9634", pca9634 }, |
|---|
| 106 | | - { "PCA9635", pca9635 }, |
|---|
| 107 | | - { } |
|---|
| 108 | | -}; |
|---|
| 109 | | -MODULE_DEVICE_TABLE(acpi, pca963x_acpi_ids); |
|---|
| 110 | | - |
|---|
| 111 | | -struct pca963x_led; |
|---|
| 112 | | - |
|---|
| 113 | | -struct pca963x { |
|---|
| 114 | | - struct pca963x_chipdef *chipdef; |
|---|
| 115 | | - struct mutex mutex; |
|---|
| 116 | | - struct i2c_client *client; |
|---|
| 117 | | - struct pca963x_led *leds; |
|---|
| 118 | | - unsigned long leds_on; |
|---|
| 119 | | -}; |
|---|
| 98 | +struct pca963x; |
|---|
| 120 | 99 | |
|---|
| 121 | 100 | struct pca963x_led { |
|---|
| 122 | 101 | struct pca963x *chip; |
|---|
| 123 | 102 | struct led_classdev led_cdev; |
|---|
| 124 | 103 | int led_num; /* 0 .. 15 potentially */ |
|---|
| 125 | | - char name[32]; |
|---|
| 126 | 104 | u8 gdc; |
|---|
| 127 | 105 | u8 gfrq; |
|---|
| 128 | 106 | }; |
|---|
| 129 | 107 | |
|---|
| 130 | | -static int pca963x_brightness(struct pca963x_led *pca963x, |
|---|
| 131 | | - enum led_brightness brightness) |
|---|
| 108 | +struct pca963x { |
|---|
| 109 | + struct pca963x_chipdef *chipdef; |
|---|
| 110 | + struct mutex mutex; |
|---|
| 111 | + struct i2c_client *client; |
|---|
| 112 | + unsigned long leds_on; |
|---|
| 113 | + struct pca963x_led leds[]; |
|---|
| 114 | +}; |
|---|
| 115 | + |
|---|
| 116 | +static int pca963x_brightness(struct pca963x_led *led, |
|---|
| 117 | + enum led_brightness brightness) |
|---|
| 132 | 118 | { |
|---|
| 133 | | - u8 ledout_addr = pca963x->chip->chipdef->ledout_base |
|---|
| 134 | | - + (pca963x->led_num / 4); |
|---|
| 135 | | - u8 ledout; |
|---|
| 136 | | - int shift = 2 * (pca963x->led_num % 4); |
|---|
| 137 | | - u8 mask = 0x3 << shift; |
|---|
| 119 | + struct i2c_client *client = led->chip->client; |
|---|
| 120 | + struct pca963x_chipdef *chipdef = led->chip->chipdef; |
|---|
| 121 | + u8 ledout_addr, ledout, mask, val; |
|---|
| 122 | + int shift; |
|---|
| 138 | 123 | int ret; |
|---|
| 139 | 124 | |
|---|
| 140 | | - ledout = i2c_smbus_read_byte_data(pca963x->chip->client, ledout_addr); |
|---|
| 125 | + ledout_addr = chipdef->ledout_base + (led->led_num / 4); |
|---|
| 126 | + shift = 2 * (led->led_num % 4); |
|---|
| 127 | + mask = 0x3 << shift; |
|---|
| 128 | + ledout = i2c_smbus_read_byte_data(client, ledout_addr); |
|---|
| 129 | + |
|---|
| 141 | 130 | switch (brightness) { |
|---|
| 142 | 131 | case LED_FULL: |
|---|
| 143 | | - ret = i2c_smbus_write_byte_data(pca963x->chip->client, |
|---|
| 144 | | - ledout_addr, |
|---|
| 145 | | - (ledout & ~mask) | (PCA963X_LED_ON << shift)); |
|---|
| 132 | + val = (ledout & ~mask) | (PCA963X_LED_ON << shift); |
|---|
| 133 | + ret = i2c_smbus_write_byte_data(client, ledout_addr, val); |
|---|
| 146 | 134 | break; |
|---|
| 147 | 135 | case LED_OFF: |
|---|
| 148 | | - ret = i2c_smbus_write_byte_data(pca963x->chip->client, |
|---|
| 149 | | - ledout_addr, ledout & ~mask); |
|---|
| 136 | + val = ledout & ~mask; |
|---|
| 137 | + ret = i2c_smbus_write_byte_data(client, ledout_addr, val); |
|---|
| 150 | 138 | break; |
|---|
| 151 | 139 | default: |
|---|
| 152 | | - ret = i2c_smbus_write_byte_data(pca963x->chip->client, |
|---|
| 153 | | - PCA963X_PWM_BASE + pca963x->led_num, |
|---|
| 154 | | - brightness); |
|---|
| 140 | + ret = i2c_smbus_write_byte_data(client, |
|---|
| 141 | + PCA963X_PWM_BASE + |
|---|
| 142 | + led->led_num, |
|---|
| 143 | + brightness); |
|---|
| 155 | 144 | if (ret < 0) |
|---|
| 156 | 145 | return ret; |
|---|
| 157 | | - ret = i2c_smbus_write_byte_data(pca963x->chip->client, |
|---|
| 158 | | - ledout_addr, |
|---|
| 159 | | - (ledout & ~mask) | (PCA963X_LED_PWM << shift)); |
|---|
| 146 | + |
|---|
| 147 | + val = (ledout & ~mask) | (PCA963X_LED_PWM << shift); |
|---|
| 148 | + ret = i2c_smbus_write_byte_data(client, ledout_addr, val); |
|---|
| 160 | 149 | break; |
|---|
| 161 | 150 | } |
|---|
| 162 | 151 | |
|---|
| 163 | 152 | return ret; |
|---|
| 164 | 153 | } |
|---|
| 165 | 154 | |
|---|
| 166 | | -static void pca963x_blink(struct pca963x_led *pca963x) |
|---|
| 155 | +static void pca963x_blink(struct pca963x_led *led) |
|---|
| 167 | 156 | { |
|---|
| 168 | | - u8 ledout_addr = pca963x->chip->chipdef->ledout_base + |
|---|
| 169 | | - (pca963x->led_num / 4); |
|---|
| 170 | | - u8 ledout; |
|---|
| 171 | | - u8 mode2 = i2c_smbus_read_byte_data(pca963x->chip->client, |
|---|
| 172 | | - PCA963X_MODE2); |
|---|
| 173 | | - int shift = 2 * (pca963x->led_num % 4); |
|---|
| 174 | | - u8 mask = 0x3 << shift; |
|---|
| 157 | + struct i2c_client *client = led->chip->client; |
|---|
| 158 | + struct pca963x_chipdef *chipdef = led->chip->chipdef; |
|---|
| 159 | + u8 ledout_addr, ledout, mask, val, mode2; |
|---|
| 160 | + int shift; |
|---|
| 175 | 161 | |
|---|
| 176 | | - i2c_smbus_write_byte_data(pca963x->chip->client, |
|---|
| 177 | | - pca963x->chip->chipdef->grppwm, pca963x->gdc); |
|---|
| 162 | + ledout_addr = chipdef->ledout_base + (led->led_num / 4); |
|---|
| 163 | + shift = 2 * (led->led_num % 4); |
|---|
| 164 | + mask = 0x3 << shift; |
|---|
| 165 | + mode2 = i2c_smbus_read_byte_data(client, PCA963X_MODE2); |
|---|
| 178 | 166 | |
|---|
| 179 | | - i2c_smbus_write_byte_data(pca963x->chip->client, |
|---|
| 180 | | - pca963x->chip->chipdef->grpfreq, pca963x->gfrq); |
|---|
| 167 | + i2c_smbus_write_byte_data(client, chipdef->grppwm, led->gdc); |
|---|
| 168 | + |
|---|
| 169 | + i2c_smbus_write_byte_data(client, chipdef->grpfreq, led->gfrq); |
|---|
| 181 | 170 | |
|---|
| 182 | 171 | if (!(mode2 & PCA963X_MODE2_DMBLNK)) |
|---|
| 183 | | - i2c_smbus_write_byte_data(pca963x->chip->client, PCA963X_MODE2, |
|---|
| 184 | | - mode2 | PCA963X_MODE2_DMBLNK); |
|---|
| 172 | + i2c_smbus_write_byte_data(client, PCA963X_MODE2, |
|---|
| 173 | + mode2 | PCA963X_MODE2_DMBLNK); |
|---|
| 185 | 174 | |
|---|
| 186 | | - mutex_lock(&pca963x->chip->mutex); |
|---|
| 187 | | - ledout = i2c_smbus_read_byte_data(pca963x->chip->client, ledout_addr); |
|---|
| 188 | | - if ((ledout & mask) != (PCA963X_LED_GRP_PWM << shift)) |
|---|
| 189 | | - i2c_smbus_write_byte_data(pca963x->chip->client, ledout_addr, |
|---|
| 190 | | - (ledout & ~mask) | (PCA963X_LED_GRP_PWM << shift)); |
|---|
| 191 | | - mutex_unlock(&pca963x->chip->mutex); |
|---|
| 175 | + mutex_lock(&led->chip->mutex); |
|---|
| 176 | + |
|---|
| 177 | + ledout = i2c_smbus_read_byte_data(client, ledout_addr); |
|---|
| 178 | + if ((ledout & mask) != (PCA963X_LED_GRP_PWM << shift)) { |
|---|
| 179 | + val = (ledout & ~mask) | (PCA963X_LED_GRP_PWM << shift); |
|---|
| 180 | + i2c_smbus_write_byte_data(client, ledout_addr, val); |
|---|
| 181 | + } |
|---|
| 182 | + |
|---|
| 183 | + mutex_unlock(&led->chip->mutex); |
|---|
| 192 | 184 | } |
|---|
| 193 | 185 | |
|---|
| 194 | | -static int pca963x_power_state(struct pca963x_led *pca963x) |
|---|
| 186 | +static int pca963x_power_state(struct pca963x_led *led) |
|---|
| 195 | 187 | { |
|---|
| 196 | | - unsigned long *leds_on = &pca963x->chip->leds_on; |
|---|
| 197 | | - unsigned long cached_leds = pca963x->chip->leds_on; |
|---|
| 188 | + struct i2c_client *client = led->chip->client; |
|---|
| 189 | + unsigned long *leds_on = &led->chip->leds_on; |
|---|
| 190 | + unsigned long cached_leds = *leds_on; |
|---|
| 198 | 191 | |
|---|
| 199 | | - if (pca963x->led_cdev.brightness) |
|---|
| 200 | | - set_bit(pca963x->led_num, leds_on); |
|---|
| 192 | + if (led->led_cdev.brightness) |
|---|
| 193 | + set_bit(led->led_num, leds_on); |
|---|
| 201 | 194 | else |
|---|
| 202 | | - clear_bit(pca963x->led_num, leds_on); |
|---|
| 195 | + clear_bit(led->led_num, leds_on); |
|---|
| 203 | 196 | |
|---|
| 204 | 197 | if (!(*leds_on) != !cached_leds) |
|---|
| 205 | | - return i2c_smbus_write_byte_data(pca963x->chip->client, |
|---|
| 206 | | - PCA963X_MODE1, *leds_on ? 0 : BIT(4)); |
|---|
| 198 | + return i2c_smbus_write_byte_data(client, PCA963X_MODE1, |
|---|
| 199 | + *leds_on ? 0 : BIT(4)); |
|---|
| 207 | 200 | |
|---|
| 208 | 201 | return 0; |
|---|
| 209 | 202 | } |
|---|
| 210 | 203 | |
|---|
| 211 | 204 | static int pca963x_led_set(struct led_classdev *led_cdev, |
|---|
| 212 | | - enum led_brightness value) |
|---|
| 205 | + enum led_brightness value) |
|---|
| 213 | 206 | { |
|---|
| 214 | | - struct pca963x_led *pca963x; |
|---|
| 207 | + struct pca963x_led *led; |
|---|
| 215 | 208 | int ret; |
|---|
| 216 | 209 | |
|---|
| 217 | | - pca963x = container_of(led_cdev, struct pca963x_led, led_cdev); |
|---|
| 210 | + led = container_of(led_cdev, struct pca963x_led, led_cdev); |
|---|
| 218 | 211 | |
|---|
| 219 | | - mutex_lock(&pca963x->chip->mutex); |
|---|
| 212 | + mutex_lock(&led->chip->mutex); |
|---|
| 220 | 213 | |
|---|
| 221 | | - ret = pca963x_brightness(pca963x, value); |
|---|
| 214 | + ret = pca963x_brightness(led, value); |
|---|
| 222 | 215 | if (ret < 0) |
|---|
| 223 | 216 | goto unlock; |
|---|
| 224 | | - ret = pca963x_power_state(pca963x); |
|---|
| 217 | + ret = pca963x_power_state(led); |
|---|
| 225 | 218 | |
|---|
| 226 | 219 | unlock: |
|---|
| 227 | | - mutex_unlock(&pca963x->chip->mutex); |
|---|
| 220 | + mutex_unlock(&led->chip->mutex); |
|---|
| 228 | 221 | return ret; |
|---|
| 229 | 222 | } |
|---|
| 230 | 223 | |
|---|
| 231 | | -static unsigned int pca963x_period_scale(struct pca963x_led *pca963x, |
|---|
| 232 | | - unsigned int val) |
|---|
| 224 | +static unsigned int pca963x_period_scale(struct pca963x_led *led, |
|---|
| 225 | + unsigned int val) |
|---|
| 233 | 226 | { |
|---|
| 234 | | - unsigned int scaling = pca963x->chip->chipdef->scaling; |
|---|
| 227 | + unsigned int scaling = led->chip->chipdef->scaling; |
|---|
| 235 | 228 | |
|---|
| 236 | 229 | return scaling ? DIV_ROUND_CLOSEST(val * scaling, 1000) : val; |
|---|
| 237 | 230 | } |
|---|
| 238 | 231 | |
|---|
| 239 | 232 | static int pca963x_blink_set(struct led_classdev *led_cdev, |
|---|
| 240 | | - unsigned long *delay_on, unsigned long *delay_off) |
|---|
| 233 | + unsigned long *delay_on, unsigned long *delay_off) |
|---|
| 241 | 234 | { |
|---|
| 242 | | - struct pca963x_led *pca963x; |
|---|
| 243 | 235 | unsigned long time_on, time_off, period; |
|---|
| 236 | + struct pca963x_led *led; |
|---|
| 244 | 237 | u8 gdc, gfrq; |
|---|
| 245 | 238 | |
|---|
| 246 | | - pca963x = container_of(led_cdev, struct pca963x_led, led_cdev); |
|---|
| 239 | + led = container_of(led_cdev, struct pca963x_led, led_cdev); |
|---|
| 247 | 240 | |
|---|
| 248 | 241 | time_on = *delay_on; |
|---|
| 249 | 242 | time_off = *delay_off; |
|---|
| .. | .. |
|---|
| 254 | 247 | time_off = 500; |
|---|
| 255 | 248 | } |
|---|
| 256 | 249 | |
|---|
| 257 | | - period = pca963x_period_scale(pca963x, time_on + time_off); |
|---|
| 250 | + period = pca963x_period_scale(led, time_on + time_off); |
|---|
| 258 | 251 | |
|---|
| 259 | 252 | /* If period not supported by hardware, default to someting sane. */ |
|---|
| 260 | 253 | if ((period < PCA963X_BLINK_PERIOD_MIN) || |
|---|
| 261 | 254 | (period > PCA963X_BLINK_PERIOD_MAX)) { |
|---|
| 262 | 255 | time_on = 500; |
|---|
| 263 | 256 | time_off = 500; |
|---|
| 264 | | - period = pca963x_period_scale(pca963x, 1000); |
|---|
| 257 | + period = pca963x_period_scale(led, 1000); |
|---|
| 265 | 258 | } |
|---|
| 266 | 259 | |
|---|
| 267 | 260 | /* |
|---|
| .. | .. |
|---|
| 269 | 262 | * (time_on / period) = (GDC / 256) -> |
|---|
| 270 | 263 | * GDC = ((time_on * 256) / period) |
|---|
| 271 | 264 | */ |
|---|
| 272 | | - gdc = (pca963x_period_scale(pca963x, time_on) * 256) / period; |
|---|
| 265 | + gdc = (pca963x_period_scale(led, time_on) * 256) / period; |
|---|
| 273 | 266 | |
|---|
| 274 | 267 | /* |
|---|
| 275 | 268 | * From manual: period = ((GFRQ + 1) / 24) in seconds. |
|---|
| .. | .. |
|---|
| 278 | 271 | */ |
|---|
| 279 | 272 | gfrq = (period * 24 / 1000) - 1; |
|---|
| 280 | 273 | |
|---|
| 281 | | - pca963x->gdc = gdc; |
|---|
| 282 | | - pca963x->gfrq = gfrq; |
|---|
| 274 | + led->gdc = gdc; |
|---|
| 275 | + led->gfrq = gfrq; |
|---|
| 283 | 276 | |
|---|
| 284 | | - pca963x_blink(pca963x); |
|---|
| 277 | + pca963x_blink(led); |
|---|
| 285 | 278 | |
|---|
| 286 | 279 | *delay_on = time_on; |
|---|
| 287 | 280 | *delay_off = time_off; |
|---|
| .. | .. |
|---|
| 289 | 282 | return 0; |
|---|
| 290 | 283 | } |
|---|
| 291 | 284 | |
|---|
| 292 | | -#if IS_ENABLED(CONFIG_OF) |
|---|
| 293 | | -static struct pca963x_platform_data * |
|---|
| 294 | | -pca963x_dt_init(struct i2c_client *client, struct pca963x_chipdef *chip) |
|---|
| 285 | +static int pca963x_register_leds(struct i2c_client *client, |
|---|
| 286 | + struct pca963x *chip) |
|---|
| 295 | 287 | { |
|---|
| 296 | | - struct device_node *np = client->dev.of_node, *child; |
|---|
| 297 | | - struct pca963x_platform_data *pdata; |
|---|
| 298 | | - struct led_info *pca963x_leds; |
|---|
| 299 | | - int count; |
|---|
| 288 | + struct pca963x_chipdef *chipdef = chip->chipdef; |
|---|
| 289 | + struct pca963x_led *led = chip->leds; |
|---|
| 290 | + struct device *dev = &client->dev; |
|---|
| 291 | + struct fwnode_handle *child; |
|---|
| 292 | + bool hw_blink; |
|---|
| 293 | + s32 mode2; |
|---|
| 294 | + u32 reg; |
|---|
| 295 | + int ret; |
|---|
| 300 | 296 | |
|---|
| 301 | | - count = of_get_child_count(np); |
|---|
| 302 | | - if (!count || count > chip->n_leds) |
|---|
| 303 | | - return ERR_PTR(-ENODEV); |
|---|
| 297 | + if (device_property_read_u32(dev, "nxp,period-scale", |
|---|
| 298 | + &chipdef->scaling)) |
|---|
| 299 | + chipdef->scaling = 1000; |
|---|
| 304 | 300 | |
|---|
| 305 | | - pca963x_leds = devm_kcalloc(&client->dev, |
|---|
| 306 | | - chip->n_leds, sizeof(struct led_info), GFP_KERNEL); |
|---|
| 307 | | - if (!pca963x_leds) |
|---|
| 308 | | - return ERR_PTR(-ENOMEM); |
|---|
| 301 | + hw_blink = device_property_read_bool(dev, "nxp,hw-blink"); |
|---|
| 309 | 302 | |
|---|
| 310 | | - for_each_child_of_node(np, child) { |
|---|
| 311 | | - struct led_info led = {}; |
|---|
| 312 | | - u32 reg; |
|---|
| 313 | | - int res; |
|---|
| 314 | | - |
|---|
| 315 | | - res = of_property_read_u32(child, "reg", ®); |
|---|
| 316 | | - if ((res != 0) || (reg >= chip->n_leds)) |
|---|
| 317 | | - continue; |
|---|
| 318 | | - led.name = |
|---|
| 319 | | - of_get_property(child, "label", NULL) ? : child->name; |
|---|
| 320 | | - led.default_trigger = |
|---|
| 321 | | - of_get_property(child, "linux,default-trigger", NULL); |
|---|
| 322 | | - pca963x_leds[reg] = led; |
|---|
| 323 | | - } |
|---|
| 324 | | - pdata = devm_kzalloc(&client->dev, |
|---|
| 325 | | - sizeof(struct pca963x_platform_data), GFP_KERNEL); |
|---|
| 326 | | - if (!pdata) |
|---|
| 327 | | - return ERR_PTR(-ENOMEM); |
|---|
| 328 | | - |
|---|
| 329 | | - pdata->leds.leds = pca963x_leds; |
|---|
| 330 | | - pdata->leds.num_leds = chip->n_leds; |
|---|
| 303 | + mode2 = i2c_smbus_read_byte_data(client, PCA963X_MODE2); |
|---|
| 304 | + if (mode2 < 0) |
|---|
| 305 | + return mode2; |
|---|
| 331 | 306 | |
|---|
| 332 | 307 | /* default to open-drain unless totem pole (push-pull) is specified */ |
|---|
| 333 | | - if (of_property_read_bool(np, "nxp,totem-pole")) |
|---|
| 334 | | - pdata->outdrv = PCA963X_TOTEM_POLE; |
|---|
| 308 | + if (device_property_read_bool(dev, "nxp,totem-pole")) |
|---|
| 309 | + mode2 |= PCA963X_MODE2_OUTDRV; |
|---|
| 335 | 310 | else |
|---|
| 336 | | - pdata->outdrv = PCA963X_OPEN_DRAIN; |
|---|
| 337 | | - |
|---|
| 338 | | - /* default to software blinking unless hardware blinking is specified */ |
|---|
| 339 | | - if (of_property_read_bool(np, "nxp,hw-blink")) |
|---|
| 340 | | - pdata->blink_type = PCA963X_HW_BLINK; |
|---|
| 341 | | - else |
|---|
| 342 | | - pdata->blink_type = PCA963X_SW_BLINK; |
|---|
| 343 | | - |
|---|
| 344 | | - if (of_property_read_u32(np, "nxp,period-scale", &chip->scaling)) |
|---|
| 345 | | - chip->scaling = 1000; |
|---|
| 311 | + mode2 &= ~PCA963X_MODE2_OUTDRV; |
|---|
| 346 | 312 | |
|---|
| 347 | 313 | /* default to non-inverted output, unless inverted is specified */ |
|---|
| 348 | | - if (of_property_read_bool(np, "nxp,inverted-out")) |
|---|
| 349 | | - pdata->dir = PCA963X_INVERTED; |
|---|
| 314 | + if (device_property_read_bool(dev, "nxp,inverted-out")) |
|---|
| 315 | + mode2 |= PCA963X_MODE2_INVRT; |
|---|
| 350 | 316 | else |
|---|
| 351 | | - pdata->dir = PCA963X_NORMAL; |
|---|
| 317 | + mode2 &= ~PCA963X_MODE2_INVRT; |
|---|
| 352 | 318 | |
|---|
| 353 | | - return pdata; |
|---|
| 319 | + ret = i2c_smbus_write_byte_data(client, PCA963X_MODE2, mode2); |
|---|
| 320 | + if (ret < 0) |
|---|
| 321 | + return ret; |
|---|
| 322 | + |
|---|
| 323 | + device_for_each_child_node(dev, child) { |
|---|
| 324 | + struct led_init_data init_data = {}; |
|---|
| 325 | + char default_label[32]; |
|---|
| 326 | + |
|---|
| 327 | + ret = fwnode_property_read_u32(child, "reg", ®); |
|---|
| 328 | + if (ret || reg >= chipdef->n_leds) { |
|---|
| 329 | + dev_err(dev, "Invalid 'reg' property for node %pfw\n", |
|---|
| 330 | + child); |
|---|
| 331 | + ret = -EINVAL; |
|---|
| 332 | + goto err; |
|---|
| 333 | + } |
|---|
| 334 | + |
|---|
| 335 | + led->led_num = reg; |
|---|
| 336 | + led->chip = chip; |
|---|
| 337 | + led->led_cdev.brightness_set_blocking = pca963x_led_set; |
|---|
| 338 | + if (hw_blink) |
|---|
| 339 | + led->led_cdev.blink_set = pca963x_blink_set; |
|---|
| 340 | + |
|---|
| 341 | + init_data.fwnode = child; |
|---|
| 342 | + /* for backwards compatibility */ |
|---|
| 343 | + init_data.devicename = "pca963x"; |
|---|
| 344 | + snprintf(default_label, sizeof(default_label), "%d:%.2x:%u", |
|---|
| 345 | + client->adapter->nr, client->addr, reg); |
|---|
| 346 | + init_data.default_label = default_label; |
|---|
| 347 | + |
|---|
| 348 | + ret = devm_led_classdev_register_ext(dev, &led->led_cdev, |
|---|
| 349 | + &init_data); |
|---|
| 350 | + if (ret) { |
|---|
| 351 | + dev_err(dev, "Failed to register LED for node %pfw\n", |
|---|
| 352 | + child); |
|---|
| 353 | + goto err; |
|---|
| 354 | + } |
|---|
| 355 | + |
|---|
| 356 | + ++led; |
|---|
| 357 | + } |
|---|
| 358 | + |
|---|
| 359 | + return 0; |
|---|
| 360 | +err: |
|---|
| 361 | + fwnode_handle_put(child); |
|---|
| 362 | + return ret; |
|---|
| 354 | 363 | } |
|---|
| 355 | 364 | |
|---|
| 356 | 365 | static const struct of_device_id of_pca963x_match[] = { |
|---|
| .. | .. |
|---|
| 361 | 370 | {}, |
|---|
| 362 | 371 | }; |
|---|
| 363 | 372 | MODULE_DEVICE_TABLE(of, of_pca963x_match); |
|---|
| 364 | | -#else |
|---|
| 365 | | -static struct pca963x_platform_data * |
|---|
| 366 | | -pca963x_dt_init(struct i2c_client *client, struct pca963x_chipdef *chip) |
|---|
| 367 | | -{ |
|---|
| 368 | | - return ERR_PTR(-ENODEV); |
|---|
| 369 | | -} |
|---|
| 370 | | -#endif |
|---|
| 371 | 373 | |
|---|
| 372 | 374 | static int pca963x_probe(struct i2c_client *client, |
|---|
| 373 | | - const struct i2c_device_id *id) |
|---|
| 375 | + const struct i2c_device_id *id) |
|---|
| 374 | 376 | { |
|---|
| 375 | | - struct pca963x *pca963x_chip; |
|---|
| 376 | | - struct pca963x_led *pca963x; |
|---|
| 377 | | - struct pca963x_platform_data *pdata; |
|---|
| 378 | | - struct pca963x_chipdef *chip; |
|---|
| 379 | | - int i, err; |
|---|
| 377 | + struct device *dev = &client->dev; |
|---|
| 378 | + struct pca963x_chipdef *chipdef; |
|---|
| 379 | + struct pca963x *chip; |
|---|
| 380 | + int i, count; |
|---|
| 380 | 381 | |
|---|
| 381 | | - if (id) { |
|---|
| 382 | | - chip = &pca963x_chipdefs[id->driver_data]; |
|---|
| 383 | | - } else { |
|---|
| 384 | | - const struct acpi_device_id *acpi_id; |
|---|
| 382 | + chipdef = &pca963x_chipdefs[id->driver_data]; |
|---|
| 385 | 383 | |
|---|
| 386 | | - acpi_id = acpi_match_device(pca963x_acpi_ids, &client->dev); |
|---|
| 387 | | - if (!acpi_id) |
|---|
| 388 | | - return -ENODEV; |
|---|
| 389 | | - chip = &pca963x_chipdefs[acpi_id->driver_data]; |
|---|
| 390 | | - } |
|---|
| 391 | | - pdata = dev_get_platdata(&client->dev); |
|---|
| 392 | | - |
|---|
| 393 | | - if (!pdata) { |
|---|
| 394 | | - pdata = pca963x_dt_init(client, chip); |
|---|
| 395 | | - if (IS_ERR(pdata)) { |
|---|
| 396 | | - dev_warn(&client->dev, "could not parse configuration\n"); |
|---|
| 397 | | - pdata = NULL; |
|---|
| 398 | | - } |
|---|
| 399 | | - } |
|---|
| 400 | | - |
|---|
| 401 | | - if (pdata && (pdata->leds.num_leds < 1 || |
|---|
| 402 | | - pdata->leds.num_leds > chip->n_leds)) { |
|---|
| 403 | | - dev_err(&client->dev, "board info must claim 1-%d LEDs", |
|---|
| 404 | | - chip->n_leds); |
|---|
| 384 | + count = device_get_child_node_count(dev); |
|---|
| 385 | + if (!count || count > chipdef->n_leds) { |
|---|
| 386 | + dev_err(dev, "Node %pfw must define between 1 and %d LEDs\n", |
|---|
| 387 | + dev_fwnode(dev), chipdef->n_leds); |
|---|
| 405 | 388 | return -EINVAL; |
|---|
| 406 | 389 | } |
|---|
| 407 | 390 | |
|---|
| 408 | | - pca963x_chip = devm_kzalloc(&client->dev, sizeof(*pca963x_chip), |
|---|
| 409 | | - GFP_KERNEL); |
|---|
| 410 | | - if (!pca963x_chip) |
|---|
| 411 | | - return -ENOMEM; |
|---|
| 412 | | - pca963x = devm_kcalloc(&client->dev, chip->n_leds, sizeof(*pca963x), |
|---|
| 413 | | - GFP_KERNEL); |
|---|
| 414 | | - if (!pca963x) |
|---|
| 391 | + chip = devm_kzalloc(dev, struct_size(chip, leds, count), GFP_KERNEL); |
|---|
| 392 | + if (!chip) |
|---|
| 415 | 393 | return -ENOMEM; |
|---|
| 416 | 394 | |
|---|
| 417 | | - i2c_set_clientdata(client, pca963x_chip); |
|---|
| 395 | + i2c_set_clientdata(client, chip); |
|---|
| 418 | 396 | |
|---|
| 419 | | - mutex_init(&pca963x_chip->mutex); |
|---|
| 420 | | - pca963x_chip->chipdef = chip; |
|---|
| 421 | | - pca963x_chip->client = client; |
|---|
| 422 | | - pca963x_chip->leds = pca963x; |
|---|
| 397 | + mutex_init(&chip->mutex); |
|---|
| 398 | + chip->chipdef = chipdef; |
|---|
| 399 | + chip->client = client; |
|---|
| 423 | 400 | |
|---|
| 424 | 401 | /* Turn off LEDs by default*/ |
|---|
| 425 | | - for (i = 0; i < chip->n_leds / 4; i++) |
|---|
| 426 | | - i2c_smbus_write_byte_data(client, chip->ledout_base + i, 0x00); |
|---|
| 427 | | - |
|---|
| 428 | | - for (i = 0; i < chip->n_leds; i++) { |
|---|
| 429 | | - pca963x[i].led_num = i; |
|---|
| 430 | | - pca963x[i].chip = pca963x_chip; |
|---|
| 431 | | - |
|---|
| 432 | | - /* Platform data can specify LED names and default triggers */ |
|---|
| 433 | | - if (pdata && i < pdata->leds.num_leds) { |
|---|
| 434 | | - if (pdata->leds.leds[i].name) |
|---|
| 435 | | - snprintf(pca963x[i].name, |
|---|
| 436 | | - sizeof(pca963x[i].name), "pca963x:%s", |
|---|
| 437 | | - pdata->leds.leds[i].name); |
|---|
| 438 | | - if (pdata->leds.leds[i].default_trigger) |
|---|
| 439 | | - pca963x[i].led_cdev.default_trigger = |
|---|
| 440 | | - pdata->leds.leds[i].default_trigger; |
|---|
| 441 | | - } |
|---|
| 442 | | - if (!pdata || i >= pdata->leds.num_leds || |
|---|
| 443 | | - !pdata->leds.leds[i].name) |
|---|
| 444 | | - snprintf(pca963x[i].name, sizeof(pca963x[i].name), |
|---|
| 445 | | - "pca963x:%d:%.2x:%d", client->adapter->nr, |
|---|
| 446 | | - client->addr, i); |
|---|
| 447 | | - |
|---|
| 448 | | - pca963x[i].led_cdev.name = pca963x[i].name; |
|---|
| 449 | | - pca963x[i].led_cdev.brightness_set_blocking = pca963x_led_set; |
|---|
| 450 | | - |
|---|
| 451 | | - if (pdata && pdata->blink_type == PCA963X_HW_BLINK) |
|---|
| 452 | | - pca963x[i].led_cdev.blink_set = pca963x_blink_set; |
|---|
| 453 | | - |
|---|
| 454 | | - err = led_classdev_register(&client->dev, &pca963x[i].led_cdev); |
|---|
| 455 | | - if (err < 0) |
|---|
| 456 | | - goto exit; |
|---|
| 457 | | - } |
|---|
| 402 | + for (i = 0; i < chipdef->n_leds / 4; i++) |
|---|
| 403 | + i2c_smbus_write_byte_data(client, chipdef->ledout_base + i, 0x00); |
|---|
| 458 | 404 | |
|---|
| 459 | 405 | /* Disable LED all-call address, and power down initially */ |
|---|
| 460 | 406 | i2c_smbus_write_byte_data(client, PCA963X_MODE1, BIT(4)); |
|---|
| 461 | 407 | |
|---|
| 462 | | - if (pdata) { |
|---|
| 463 | | - u8 mode2 = i2c_smbus_read_byte_data(pca963x->chip->client, |
|---|
| 464 | | - PCA963X_MODE2); |
|---|
| 465 | | - /* Configure output: open-drain or totem pole (push-pull) */ |
|---|
| 466 | | - if (pdata->outdrv == PCA963X_OPEN_DRAIN) |
|---|
| 467 | | - mode2 &= ~PCA963X_MODE2_OUTDRV; |
|---|
| 468 | | - else |
|---|
| 469 | | - mode2 |= PCA963X_MODE2_OUTDRV; |
|---|
| 470 | | - /* Configure direction: normal or inverted */ |
|---|
| 471 | | - if (pdata->dir == PCA963X_INVERTED) |
|---|
| 472 | | - mode2 |= PCA963X_MODE2_INVRT; |
|---|
| 473 | | - i2c_smbus_write_byte_data(pca963x->chip->client, PCA963X_MODE2, |
|---|
| 474 | | - mode2); |
|---|
| 475 | | - } |
|---|
| 476 | | - |
|---|
| 477 | | - return 0; |
|---|
| 478 | | - |
|---|
| 479 | | -exit: |
|---|
| 480 | | - while (i--) |
|---|
| 481 | | - led_classdev_unregister(&pca963x[i].led_cdev); |
|---|
| 482 | | - |
|---|
| 483 | | - return err; |
|---|
| 484 | | -} |
|---|
| 485 | | - |
|---|
| 486 | | -static int pca963x_remove(struct i2c_client *client) |
|---|
| 487 | | -{ |
|---|
| 488 | | - struct pca963x *pca963x = i2c_get_clientdata(client); |
|---|
| 489 | | - int i; |
|---|
| 490 | | - |
|---|
| 491 | | - for (i = 0; i < pca963x->chipdef->n_leds; i++) |
|---|
| 492 | | - led_classdev_unregister(&pca963x->leds[i].led_cdev); |
|---|
| 493 | | - |
|---|
| 494 | | - return 0; |
|---|
| 408 | + return pca963x_register_leds(client, chip); |
|---|
| 495 | 409 | } |
|---|
| 496 | 410 | |
|---|
| 497 | 411 | static struct i2c_driver pca963x_driver = { |
|---|
| 498 | 412 | .driver = { |
|---|
| 499 | 413 | .name = "leds-pca963x", |
|---|
| 500 | | - .of_match_table = of_match_ptr(of_pca963x_match), |
|---|
| 501 | | - .acpi_match_table = ACPI_PTR(pca963x_acpi_ids), |
|---|
| 414 | + .of_match_table = of_pca963x_match, |
|---|
| 502 | 415 | }, |
|---|
| 503 | 416 | .probe = pca963x_probe, |
|---|
| 504 | | - .remove = pca963x_remove, |
|---|
| 505 | 417 | .id_table = pca963x_id, |
|---|
| 506 | 418 | }; |
|---|
| 507 | 419 | |
|---|