Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 bct electronic GmbH |
| 3 | * Copyright 2013 Qtechnology/AS |
| 4 | * |
| 5 | * Author: Peter Meerwald <p.meerwald@bct-electronic.com> |
| 6 | * Author: Ricardo Ribalda <ricardo.ribalda@gmail.com> |
| 7 | * |
| 8 | * 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 | * |
| 14 | * LED driver for the PCA9633 I2C LED driver (7-bit slave address 0x62) |
Peter Meerwald | 3dfedb9 | 2014-07-02 22:50:34 -0700 | [diff] [blame] | 15 | * LED driver for the PCA9634/5 I2C LED driver (7-bit slave address set by hw.) |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 16 | * |
| 17 | * Note that hardware blinking violates the leds infrastructure driver |
| 18 | * interface since the hardware only supports blinking all LEDs with the |
| 19 | * same delay_on/delay_off rates. That is, only the LEDs that are set to |
| 20 | * blink will actually blink but all LEDs that are set to blink will blink |
| 21 | * in identical fashion. The delay_on/delay_off values of the last LED |
| 22 | * that is set to blink will be used for all of the blinking LEDs. |
| 23 | * Hardware blinking is disabled by default but can be enabled by setting |
| 24 | * the 'blink_type' member in the platform_data struct to 'PCA963X_HW_BLINK' |
| 25 | * or by adding the 'nxp,hw-blink' property to the DTS. |
| 26 | */ |
| 27 | |
Tin Huynh | f26dab9 | 2016-11-29 11:18:20 +0700 | [diff] [blame] | 28 | #include <linux/acpi.h> |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 29 | #include <linux/module.h> |
| 30 | #include <linux/delay.h> |
| 31 | #include <linux/string.h> |
| 32 | #include <linux/ctype.h> |
| 33 | #include <linux/leds.h> |
| 34 | #include <linux/err.h> |
| 35 | #include <linux/i2c.h> |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 36 | #include <linux/slab.h> |
| 37 | #include <linux/of.h> |
| 38 | #include <linux/platform_data/leds-pca963x.h> |
| 39 | |
| 40 | /* LED select registers determine the source that drives LED outputs */ |
| 41 | #define PCA963X_LED_OFF 0x0 /* LED driver off */ |
| 42 | #define PCA963X_LED_ON 0x1 /* LED driver on */ |
| 43 | #define PCA963X_LED_PWM 0x2 /* Controlled through PWM */ |
| 44 | #define PCA963X_LED_GRP_PWM 0x3 /* Controlled through PWM/GRPPWM */ |
| 45 | |
| 46 | #define PCA963X_MODE2_DMBLNK 0x20 /* Enable blinking */ |
| 47 | |
| 48 | #define PCA963X_MODE1 0x00 |
| 49 | #define PCA963X_MODE2 0x01 |
| 50 | #define PCA963X_PWM_BASE 0x02 |
| 51 | |
| 52 | enum pca963x_type { |
| 53 | pca9633, |
| 54 | pca9634, |
Peter Meerwald | 3dfedb9 | 2014-07-02 22:50:34 -0700 | [diff] [blame] | 55 | pca9635, |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | struct pca963x_chipdef { |
| 59 | u8 grppwm; |
| 60 | u8 grpfreq; |
| 61 | u8 ledout_base; |
| 62 | int n_leds; |
Matt Ranostay | 35c7d30 | 2016-10-13 06:16:12 -0700 | [diff] [blame] | 63 | unsigned int scaling; |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | static struct pca963x_chipdef pca963x_chipdefs[] = { |
| 67 | [pca9633] = { |
| 68 | .grppwm = 0x6, |
| 69 | .grpfreq = 0x7, |
| 70 | .ledout_base = 0x8, |
| 71 | .n_leds = 4, |
| 72 | }, |
| 73 | [pca9634] = { |
| 74 | .grppwm = 0xa, |
| 75 | .grpfreq = 0xb, |
| 76 | .ledout_base = 0xc, |
| 77 | .n_leds = 8, |
| 78 | }, |
Peter Meerwald | 3dfedb9 | 2014-07-02 22:50:34 -0700 | [diff] [blame] | 79 | [pca9635] = { |
| 80 | .grppwm = 0x12, |
| 81 | .grpfreq = 0x13, |
| 82 | .ledout_base = 0x14, |
| 83 | .n_leds = 16, |
| 84 | }, |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | /* Total blink period in milliseconds */ |
| 88 | #define PCA963X_BLINK_PERIOD_MIN 42 |
| 89 | #define PCA963X_BLINK_PERIOD_MAX 10667 |
| 90 | |
| 91 | static const struct i2c_device_id pca963x_id[] = { |
| 92 | { "pca9632", pca9633 }, |
| 93 | { "pca9633", pca9633 }, |
| 94 | { "pca9634", pca9634 }, |
Peter Meerwald | 3dfedb9 | 2014-07-02 22:50:34 -0700 | [diff] [blame] | 95 | { "pca9635", pca9635 }, |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 96 | { } |
| 97 | }; |
| 98 | MODULE_DEVICE_TABLE(i2c, pca963x_id); |
| 99 | |
Tin Huynh | f26dab9 | 2016-11-29 11:18:20 +0700 | [diff] [blame] | 100 | static const struct acpi_device_id pca963x_acpi_ids[] = { |
| 101 | { "PCA9632", pca9633 }, |
| 102 | { "PCA9633", pca9633 }, |
| 103 | { "PCA9634", pca9634 }, |
| 104 | { "PCA9635", pca9635 }, |
| 105 | { } |
| 106 | }; |
| 107 | MODULE_DEVICE_TABLE(acpi, pca963x_acpi_ids); |
| 108 | |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 109 | struct pca963x_led; |
| 110 | |
| 111 | struct pca963x { |
| 112 | struct pca963x_chipdef *chipdef; |
| 113 | struct mutex mutex; |
| 114 | struct i2c_client *client; |
| 115 | struct pca963x_led *leds; |
Matt Ranostay | a8c170b | 2016-10-28 18:20:42 -0700 | [diff] [blame] | 116 | unsigned long leds_on; |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | struct pca963x_led { |
| 120 | struct pca963x *chip; |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 121 | struct led_classdev led_cdev; |
Peter Meerwald | 3dfedb9 | 2014-07-02 22:50:34 -0700 | [diff] [blame] | 122 | int led_num; /* 0 .. 15 potentially */ |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 123 | char name[32]; |
| 124 | u8 gdc; |
| 125 | u8 gfrq; |
| 126 | }; |
| 127 | |
Andrew Lunn | 5029a2e | 2015-08-20 12:38:44 +0200 | [diff] [blame] | 128 | static int pca963x_brightness(struct pca963x_led *pca963x, |
| 129 | enum led_brightness brightness) |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 130 | { |
| 131 | u8 ledout_addr = pca963x->chip->chipdef->ledout_base |
| 132 | + (pca963x->led_num / 4); |
| 133 | u8 ledout; |
| 134 | int shift = 2 * (pca963x->led_num % 4); |
| 135 | u8 mask = 0x3 << shift; |
Andrew Lunn | 5029a2e | 2015-08-20 12:38:44 +0200 | [diff] [blame] | 136 | int ret; |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 137 | |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 138 | ledout = i2c_smbus_read_byte_data(pca963x->chip->client, ledout_addr); |
Andrew Lunn | 5029a2e | 2015-08-20 12:38:44 +0200 | [diff] [blame] | 139 | switch (brightness) { |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 140 | case LED_FULL: |
Andrew Lunn | 5029a2e | 2015-08-20 12:38:44 +0200 | [diff] [blame] | 141 | ret = i2c_smbus_write_byte_data(pca963x->chip->client, |
| 142 | ledout_addr, |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 143 | (ledout & ~mask) | (PCA963X_LED_ON << shift)); |
| 144 | break; |
| 145 | case LED_OFF: |
Andrew Lunn | 5029a2e | 2015-08-20 12:38:44 +0200 | [diff] [blame] | 146 | ret = i2c_smbus_write_byte_data(pca963x->chip->client, |
| 147 | ledout_addr, ledout & ~mask); |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 148 | break; |
| 149 | default: |
Andrew Lunn | 5029a2e | 2015-08-20 12:38:44 +0200 | [diff] [blame] | 150 | ret = i2c_smbus_write_byte_data(pca963x->chip->client, |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 151 | PCA963X_PWM_BASE + pca963x->led_num, |
Andrew Lunn | 5029a2e | 2015-08-20 12:38:44 +0200 | [diff] [blame] | 152 | brightness); |
| 153 | if (ret < 0) |
Matt Ranostay | a8c170b | 2016-10-28 18:20:42 -0700 | [diff] [blame] | 154 | return ret; |
Andrew Lunn | 5029a2e | 2015-08-20 12:38:44 +0200 | [diff] [blame] | 155 | ret = i2c_smbus_write_byte_data(pca963x->chip->client, |
| 156 | ledout_addr, |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 157 | (ledout & ~mask) | (PCA963X_LED_PWM << shift)); |
| 158 | break; |
| 159 | } |
Matt Ranostay | a8c170b | 2016-10-28 18:20:42 -0700 | [diff] [blame] | 160 | |
Andrew Lunn | 5029a2e | 2015-08-20 12:38:44 +0200 | [diff] [blame] | 161 | return ret; |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Andrew Lunn | 5029a2e | 2015-08-20 12:38:44 +0200 | [diff] [blame] | 164 | static void pca963x_blink(struct pca963x_led *pca963x) |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 165 | { |
| 166 | u8 ledout_addr = pca963x->chip->chipdef->ledout_base + |
| 167 | (pca963x->led_num / 4); |
| 168 | u8 ledout; |
| 169 | u8 mode2 = i2c_smbus_read_byte_data(pca963x->chip->client, |
| 170 | PCA963X_MODE2); |
| 171 | int shift = 2 * (pca963x->led_num % 4); |
| 172 | u8 mask = 0x3 << shift; |
| 173 | |
| 174 | i2c_smbus_write_byte_data(pca963x->chip->client, |
| 175 | pca963x->chip->chipdef->grppwm, pca963x->gdc); |
| 176 | |
| 177 | i2c_smbus_write_byte_data(pca963x->chip->client, |
| 178 | pca963x->chip->chipdef->grpfreq, pca963x->gfrq); |
| 179 | |
| 180 | if (!(mode2 & PCA963X_MODE2_DMBLNK)) |
| 181 | i2c_smbus_write_byte_data(pca963x->chip->client, PCA963X_MODE2, |
| 182 | mode2 | PCA963X_MODE2_DMBLNK); |
| 183 | |
| 184 | mutex_lock(&pca963x->chip->mutex); |
| 185 | ledout = i2c_smbus_read_byte_data(pca963x->chip->client, ledout_addr); |
| 186 | if ((ledout & mask) != (PCA963X_LED_GRP_PWM << shift)) |
| 187 | i2c_smbus_write_byte_data(pca963x->chip->client, ledout_addr, |
| 188 | (ledout & ~mask) | (PCA963X_LED_GRP_PWM << shift)); |
| 189 | mutex_unlock(&pca963x->chip->mutex); |
| 190 | } |
| 191 | |
Matt Ranostay | a8c170b | 2016-10-28 18:20:42 -0700 | [diff] [blame] | 192 | static int pca963x_power_state(struct pca963x_led *pca963x) |
| 193 | { |
| 194 | unsigned long *leds_on = &pca963x->chip->leds_on; |
| 195 | unsigned long cached_leds = pca963x->chip->leds_on; |
| 196 | |
| 197 | if (pca963x->led_cdev.brightness) |
| 198 | set_bit(pca963x->led_num, leds_on); |
| 199 | else |
| 200 | clear_bit(pca963x->led_num, leds_on); |
| 201 | |
| 202 | if (!(*leds_on) != !cached_leds) |
| 203 | return i2c_smbus_write_byte_data(pca963x->chip->client, |
| 204 | PCA963X_MODE1, *leds_on ? 0 : BIT(4)); |
| 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
Andrew Lunn | 5029a2e | 2015-08-20 12:38:44 +0200 | [diff] [blame] | 209 | static int pca963x_led_set(struct led_classdev *led_cdev, |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 210 | enum led_brightness value) |
| 211 | { |
| 212 | struct pca963x_led *pca963x; |
Matt Ranostay | a8c170b | 2016-10-28 18:20:42 -0700 | [diff] [blame] | 213 | int ret; |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 214 | |
| 215 | pca963x = container_of(led_cdev, struct pca963x_led, led_cdev); |
| 216 | |
Matt Ranostay | a8c170b | 2016-10-28 18:20:42 -0700 | [diff] [blame] | 217 | mutex_lock(&pca963x->chip->mutex); |
| 218 | |
| 219 | ret = pca963x_brightness(pca963x, value); |
| 220 | if (ret < 0) |
| 221 | goto unlock; |
| 222 | ret = pca963x_power_state(pca963x); |
| 223 | |
| 224 | unlock: |
| 225 | mutex_unlock(&pca963x->chip->mutex); |
| 226 | return ret; |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Matt Ranostay | 35c7d30 | 2016-10-13 06:16:12 -0700 | [diff] [blame] | 229 | static unsigned int pca963x_period_scale(struct pca963x_led *pca963x, |
| 230 | unsigned int val) |
| 231 | { |
| 232 | unsigned int scaling = pca963x->chip->chipdef->scaling; |
| 233 | |
| 234 | return scaling ? DIV_ROUND_CLOSEST(val * scaling, 1000) : val; |
| 235 | } |
| 236 | |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 237 | static int pca963x_blink_set(struct led_classdev *led_cdev, |
| 238 | unsigned long *delay_on, unsigned long *delay_off) |
| 239 | { |
| 240 | struct pca963x_led *pca963x; |
| 241 | unsigned long time_on, time_off, period; |
| 242 | u8 gdc, gfrq; |
| 243 | |
| 244 | pca963x = container_of(led_cdev, struct pca963x_led, led_cdev); |
| 245 | |
| 246 | time_on = *delay_on; |
| 247 | time_off = *delay_off; |
| 248 | |
| 249 | /* If both zero, pick reasonable defaults of 500ms each */ |
| 250 | if (!time_on && !time_off) { |
| 251 | time_on = 500; |
| 252 | time_off = 500; |
| 253 | } |
| 254 | |
Matt Ranostay | 35c7d30 | 2016-10-13 06:16:12 -0700 | [diff] [blame] | 255 | period = pca963x_period_scale(pca963x, time_on + time_off); |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 256 | |
| 257 | /* If period not supported by hardware, default to someting sane. */ |
| 258 | if ((period < PCA963X_BLINK_PERIOD_MIN) || |
| 259 | (period > PCA963X_BLINK_PERIOD_MAX)) { |
| 260 | time_on = 500; |
| 261 | time_off = 500; |
Matt Ranostay | 35c7d30 | 2016-10-13 06:16:12 -0700 | [diff] [blame] | 262 | period = pca963x_period_scale(pca963x, 1000); |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | /* |
| 266 | * From manual: duty cycle = (GDC / 256) -> |
| 267 | * (time_on / period) = (GDC / 256) -> |
| 268 | * GDC = ((time_on * 256) / period) |
| 269 | */ |
Matt Ranostay | 35c7d30 | 2016-10-13 06:16:12 -0700 | [diff] [blame] | 270 | gdc = (pca963x_period_scale(pca963x, time_on) * 256) / period; |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 271 | |
| 272 | /* |
| 273 | * From manual: period = ((GFRQ + 1) / 24) in seconds. |
| 274 | * So, period (in ms) = (((GFRQ + 1) / 24) * 1000) -> |
| 275 | * GFRQ = ((period * 24 / 1000) - 1) |
| 276 | */ |
| 277 | gfrq = (period * 24 / 1000) - 1; |
| 278 | |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 279 | pca963x->gdc = gdc; |
| 280 | pca963x->gfrq = gfrq; |
| 281 | |
Andrew Lunn | 5029a2e | 2015-08-20 12:38:44 +0200 | [diff] [blame] | 282 | pca963x_blink(pca963x); |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 283 | |
| 284 | *delay_on = time_on; |
| 285 | *delay_off = time_off; |
| 286 | |
| 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | #if IS_ENABLED(CONFIG_OF) |
| 291 | static struct pca963x_platform_data * |
| 292 | pca963x_dt_init(struct i2c_client *client, struct pca963x_chipdef *chip) |
| 293 | { |
| 294 | struct device_node *np = client->dev.of_node, *child; |
| 295 | struct pca963x_platform_data *pdata; |
| 296 | struct led_info *pca963x_leds; |
| 297 | int count; |
| 298 | |
| 299 | count = of_get_child_count(np); |
| 300 | if (!count || count > chip->n_leds) |
| 301 | return ERR_PTR(-ENODEV); |
| 302 | |
| 303 | pca963x_leds = devm_kzalloc(&client->dev, |
| 304 | sizeof(struct led_info) * chip->n_leds, GFP_KERNEL); |
| 305 | if (!pca963x_leds) |
| 306 | return ERR_PTR(-ENOMEM); |
| 307 | |
| 308 | for_each_child_of_node(np, child) { |
Geert Uytterhoeven | a44b0f5 | 2015-03-15 03:41:52 -0700 | [diff] [blame] | 309 | struct led_info led = {}; |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 310 | u32 reg; |
| 311 | int res; |
| 312 | |
Ricardo Ribalda Delgado | 8a6acd64 | 2013-08-14 14:23:51 -0700 | [diff] [blame] | 313 | res = of_property_read_u32(child, "reg", ®); |
| 314 | if ((res != 0) || (reg >= chip->n_leds)) |
| 315 | continue; |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 316 | led.name = |
| 317 | of_get_property(child, "label", NULL) ? : child->name; |
| 318 | led.default_trigger = |
| 319 | of_get_property(child, "linux,default-trigger", NULL); |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 320 | pca963x_leds[reg] = led; |
| 321 | } |
| 322 | pdata = devm_kzalloc(&client->dev, |
| 323 | sizeof(struct pca963x_platform_data), GFP_KERNEL); |
| 324 | if (!pdata) |
| 325 | return ERR_PTR(-ENOMEM); |
| 326 | |
| 327 | pdata->leds.leds = pca963x_leds; |
Ricardo Ribalda Delgado | 8a6acd64 | 2013-08-14 14:23:51 -0700 | [diff] [blame] | 328 | pdata->leds.num_leds = chip->n_leds; |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 329 | |
| 330 | /* default to open-drain unless totem pole (push-pull) is specified */ |
| 331 | if (of_property_read_bool(np, "nxp,totem-pole")) |
| 332 | pdata->outdrv = PCA963X_TOTEM_POLE; |
| 333 | else |
| 334 | pdata->outdrv = PCA963X_OPEN_DRAIN; |
| 335 | |
| 336 | /* default to software blinking unless hardware blinking is specified */ |
| 337 | if (of_property_read_bool(np, "nxp,hw-blink")) |
| 338 | pdata->blink_type = PCA963X_HW_BLINK; |
| 339 | else |
| 340 | pdata->blink_type = PCA963X_SW_BLINK; |
| 341 | |
Matt Ranostay | 35c7d30 | 2016-10-13 06:16:12 -0700 | [diff] [blame] | 342 | if (of_property_read_u32(np, "nxp,period-scale", &chip->scaling)) |
| 343 | chip->scaling = 1000; |
| 344 | |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 345 | return pdata; |
| 346 | } |
| 347 | |
| 348 | static const struct of_device_id of_pca963x_match[] = { |
| 349 | { .compatible = "nxp,pca9632", }, |
| 350 | { .compatible = "nxp,pca9633", }, |
| 351 | { .compatible = "nxp,pca9634", }, |
Peter Meerwald | 3dfedb9 | 2014-07-02 22:50:34 -0700 | [diff] [blame] | 352 | { .compatible = "nxp,pca9635", }, |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 353 | {}, |
| 354 | }; |
Javier Martinez Canillas | 4d59ed8 | 2015-08-25 08:31:16 +0200 | [diff] [blame] | 355 | MODULE_DEVICE_TABLE(of, of_pca963x_match); |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 356 | #else |
| 357 | static struct pca963x_platform_data * |
| 358 | pca963x_dt_init(struct i2c_client *client, struct pca963x_chipdef *chip) |
| 359 | { |
| 360 | return ERR_PTR(-ENODEV); |
| 361 | } |
| 362 | #endif |
| 363 | |
| 364 | static int pca963x_probe(struct i2c_client *client, |
| 365 | const struct i2c_device_id *id) |
| 366 | { |
| 367 | struct pca963x *pca963x_chip; |
| 368 | struct pca963x_led *pca963x; |
| 369 | struct pca963x_platform_data *pdata; |
| 370 | struct pca963x_chipdef *chip; |
| 371 | int i, err; |
| 372 | |
Tin Huynh | f26dab9 | 2016-11-29 11:18:20 +0700 | [diff] [blame] | 373 | if (id) { |
| 374 | chip = &pca963x_chipdefs[id->driver_data]; |
| 375 | } else { |
| 376 | const struct acpi_device_id *acpi_id; |
| 377 | |
| 378 | acpi_id = acpi_match_device(pca963x_acpi_ids, &client->dev); |
| 379 | if (!acpi_id) |
| 380 | return -ENODEV; |
| 381 | chip = &pca963x_chipdefs[acpi_id->driver_data]; |
| 382 | } |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 383 | pdata = dev_get_platdata(&client->dev); |
| 384 | |
| 385 | if (!pdata) { |
| 386 | pdata = pca963x_dt_init(client, chip); |
| 387 | if (IS_ERR(pdata)) { |
| 388 | dev_warn(&client->dev, "could not parse configuration\n"); |
| 389 | pdata = NULL; |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | if (pdata && (pdata->leds.num_leds < 1 || |
| 394 | pdata->leds.num_leds > chip->n_leds)) { |
| 395 | dev_err(&client->dev, "board info must claim 1-%d LEDs", |
| 396 | chip->n_leds); |
| 397 | return -EINVAL; |
| 398 | } |
| 399 | |
| 400 | pca963x_chip = devm_kzalloc(&client->dev, sizeof(*pca963x_chip), |
| 401 | GFP_KERNEL); |
| 402 | if (!pca963x_chip) |
| 403 | return -ENOMEM; |
| 404 | pca963x = devm_kzalloc(&client->dev, chip->n_leds * sizeof(*pca963x), |
| 405 | GFP_KERNEL); |
| 406 | if (!pca963x) |
| 407 | return -ENOMEM; |
| 408 | |
| 409 | i2c_set_clientdata(client, pca963x_chip); |
| 410 | |
| 411 | mutex_init(&pca963x_chip->mutex); |
| 412 | pca963x_chip->chipdef = chip; |
| 413 | pca963x_chip->client = client; |
| 414 | pca963x_chip->leds = pca963x; |
| 415 | |
| 416 | /* Turn off LEDs by default*/ |
Peter Meerwald | 3dfedb9 | 2014-07-02 22:50:34 -0700 | [diff] [blame] | 417 | for (i = 0; i < chip->n_leds / 4; i++) |
| 418 | i2c_smbus_write_byte_data(client, chip->ledout_base + i, 0x00); |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 419 | |
| 420 | for (i = 0; i < chip->n_leds; i++) { |
| 421 | pca963x[i].led_num = i; |
| 422 | pca963x[i].chip = pca963x_chip; |
| 423 | |
| 424 | /* Platform data can specify LED names and default triggers */ |
| 425 | if (pdata && i < pdata->leds.num_leds) { |
| 426 | if (pdata->leds.leds[i].name) |
| 427 | snprintf(pca963x[i].name, |
| 428 | sizeof(pca963x[i].name), "pca963x:%s", |
| 429 | pdata->leds.leds[i].name); |
| 430 | if (pdata->leds.leds[i].default_trigger) |
| 431 | pca963x[i].led_cdev.default_trigger = |
| 432 | pdata->leds.leds[i].default_trigger; |
| 433 | } |
| 434 | if (!pdata || i >= pdata->leds.num_leds || |
| 435 | !pdata->leds.leds[i].name) |
| 436 | snprintf(pca963x[i].name, sizeof(pca963x[i].name), |
| 437 | "pca963x:%d:%.2x:%d", client->adapter->nr, |
| 438 | client->addr, i); |
| 439 | |
| 440 | pca963x[i].led_cdev.name = pca963x[i].name; |
Andrew Lunn | 5029a2e | 2015-08-20 12:38:44 +0200 | [diff] [blame] | 441 | pca963x[i].led_cdev.brightness_set_blocking = pca963x_led_set; |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 442 | |
| 443 | if (pdata && pdata->blink_type == PCA963X_HW_BLINK) |
| 444 | pca963x[i].led_cdev.blink_set = pca963x_blink_set; |
| 445 | |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 446 | err = led_classdev_register(&client->dev, &pca963x[i].led_cdev); |
| 447 | if (err < 0) |
| 448 | goto exit; |
| 449 | } |
| 450 | |
Matt Ranostay | a8c170b | 2016-10-28 18:20:42 -0700 | [diff] [blame] | 451 | /* Disable LED all-call address, and power down initially */ |
| 452 | i2c_smbus_write_byte_data(client, PCA963X_MODE1, BIT(4)); |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 453 | |
Peter Meerwald | 0c62f42 | 2014-07-02 22:50:35 -0700 | [diff] [blame] | 454 | if (pdata) { |
| 455 | /* Configure output: open-drain or totem pole (push-pull) */ |
| 456 | if (pdata->outdrv == PCA963X_OPEN_DRAIN) |
| 457 | i2c_smbus_write_byte_data(client, PCA963X_MODE2, 0x01); |
| 458 | else |
| 459 | i2c_smbus_write_byte_data(client, PCA963X_MODE2, 0x05); |
| 460 | } |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 461 | |
| 462 | return 0; |
| 463 | |
| 464 | exit: |
Andrew Lunn | 5029a2e | 2015-08-20 12:38:44 +0200 | [diff] [blame] | 465 | while (i--) |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 466 | led_classdev_unregister(&pca963x[i].led_cdev); |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 467 | |
| 468 | return err; |
| 469 | } |
| 470 | |
| 471 | static int pca963x_remove(struct i2c_client *client) |
| 472 | { |
| 473 | struct pca963x *pca963x = i2c_get_clientdata(client); |
| 474 | int i; |
| 475 | |
Andrew Lunn | 5029a2e | 2015-08-20 12:38:44 +0200 | [diff] [blame] | 476 | for (i = 0; i < pca963x->chipdef->n_leds; i++) |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 477 | led_classdev_unregister(&pca963x->leds[i].led_cdev); |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 478 | |
| 479 | return 0; |
| 480 | } |
| 481 | |
| 482 | static struct i2c_driver pca963x_driver = { |
| 483 | .driver = { |
| 484 | .name = "leds-pca963x", |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 485 | .of_match_table = of_match_ptr(of_pca963x_match), |
Tin Huynh | f26dab9 | 2016-11-29 11:18:20 +0700 | [diff] [blame] | 486 | .acpi_match_table = ACPI_PTR(pca963x_acpi_ids), |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 487 | }, |
| 488 | .probe = pca963x_probe, |
| 489 | .remove = pca963x_remove, |
| 490 | .id_table = pca963x_id, |
| 491 | }; |
| 492 | |
| 493 | module_i2c_driver(pca963x_driver); |
| 494 | |
| 495 | MODULE_AUTHOR("Peter Meerwald <p.meerwald@bct-electronic.com>"); |
| 496 | MODULE_DESCRIPTION("PCA963X LED driver"); |
| 497 | MODULE_LICENSE("GPL v2"); |