Thomas Gleixner | 36edc93 | 2019-05-29 16:57:44 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2011 bct electronic GmbH |
| 4 | * Copyright 2013 Qtechnology/AS |
| 5 | * |
| 6 | * Author: Peter Meerwald <p.meerwald@bct-electronic.com> |
Ricardo Ribalda Delgado | cea0fad | 2020-04-30 15:52:24 +0200 | [diff] [blame] | 7 | * Author: Ricardo Ribalda <ribalda@kernel.org> |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 8 | * |
| 9 | * Based on leds-pca955x.c |
| 10 | * |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 11 | * 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] | 12 | * 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] | 13 | * |
| 14 | * Note that hardware blinking violates the leds infrastructure driver |
| 15 | * interface since the hardware only supports blinking all LEDs with the |
| 16 | * same delay_on/delay_off rates. That is, only the LEDs that are set to |
| 17 | * blink will actually blink but all LEDs that are set to blink will blink |
| 18 | * in identical fashion. The delay_on/delay_off values of the last LED |
| 19 | * that is set to blink will be used for all of the blinking LEDs. |
| 20 | * Hardware blinking is disabled by default but can be enabled by setting |
| 21 | * the 'blink_type' member in the platform_data struct to 'PCA963X_HW_BLINK' |
| 22 | * or by adding the 'nxp,hw-blink' property to the DTS. |
| 23 | */ |
| 24 | |
| 25 | #include <linux/module.h> |
| 26 | #include <linux/delay.h> |
| 27 | #include <linux/string.h> |
| 28 | #include <linux/ctype.h> |
| 29 | #include <linux/leds.h> |
| 30 | #include <linux/err.h> |
| 31 | #include <linux/i2c.h> |
Andy Shevchenko | 0b6034d | 2019-03-25 16:02:07 +0200 | [diff] [blame] | 32 | #include <linux/property.h> |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 33 | #include <linux/slab.h> |
| 34 | #include <linux/of.h> |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 35 | |
| 36 | /* LED select registers determine the source that drives LED outputs */ |
| 37 | #define PCA963X_LED_OFF 0x0 /* LED driver off */ |
| 38 | #define PCA963X_LED_ON 0x1 /* LED driver on */ |
| 39 | #define PCA963X_LED_PWM 0x2 /* Controlled through PWM */ |
| 40 | #define PCA963X_LED_GRP_PWM 0x3 /* Controlled through PWM/GRPPWM */ |
| 41 | |
Zahari Petkov | 6975290 | 2019-11-18 23:02:55 +0200 | [diff] [blame] | 42 | #define PCA963X_MODE2_OUTDRV 0x04 /* Open-drain or totem pole */ |
| 43 | #define PCA963X_MODE2_INVRT 0x10 /* Normal or inverted direction */ |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 44 | #define PCA963X_MODE2_DMBLNK 0x20 /* Enable blinking */ |
| 45 | |
| 46 | #define PCA963X_MODE1 0x00 |
| 47 | #define PCA963X_MODE2 0x01 |
| 48 | #define PCA963X_PWM_BASE 0x02 |
| 49 | |
| 50 | enum pca963x_type { |
| 51 | pca9633, |
| 52 | pca9634, |
Peter Meerwald | 3dfedb9 | 2014-07-02 22:50:34 -0700 | [diff] [blame] | 53 | pca9635, |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | struct pca963x_chipdef { |
| 57 | u8 grppwm; |
| 58 | u8 grpfreq; |
| 59 | u8 ledout_base; |
| 60 | int n_leds; |
Matt Ranostay | 35c7d30 | 2016-10-13 06:16:12 -0700 | [diff] [blame] | 61 | unsigned int scaling; |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | static struct pca963x_chipdef pca963x_chipdefs[] = { |
| 65 | [pca9633] = { |
| 66 | .grppwm = 0x6, |
| 67 | .grpfreq = 0x7, |
| 68 | .ledout_base = 0x8, |
| 69 | .n_leds = 4, |
| 70 | }, |
| 71 | [pca9634] = { |
| 72 | .grppwm = 0xa, |
| 73 | .grpfreq = 0xb, |
| 74 | .ledout_base = 0xc, |
| 75 | .n_leds = 8, |
| 76 | }, |
Peter Meerwald | 3dfedb9 | 2014-07-02 22:50:34 -0700 | [diff] [blame] | 77 | [pca9635] = { |
| 78 | .grppwm = 0x12, |
| 79 | .grpfreq = 0x13, |
| 80 | .ledout_base = 0x14, |
| 81 | .n_leds = 16, |
| 82 | }, |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | /* Total blink period in milliseconds */ |
| 86 | #define PCA963X_BLINK_PERIOD_MIN 42 |
| 87 | #define PCA963X_BLINK_PERIOD_MAX 10667 |
| 88 | |
| 89 | static const struct i2c_device_id pca963x_id[] = { |
| 90 | { "pca9632", pca9633 }, |
| 91 | { "pca9633", pca9633 }, |
| 92 | { "pca9634", pca9634 }, |
Peter Meerwald | 3dfedb9 | 2014-07-02 22:50:34 -0700 | [diff] [blame] | 93 | { "pca9635", pca9635 }, |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 94 | { } |
| 95 | }; |
| 96 | MODULE_DEVICE_TABLE(i2c, pca963x_id); |
| 97 | |
Marek Behún | b5a3b44 | 2020-09-20 02:24:58 +0200 | [diff] [blame] | 98 | struct pca963x; |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 99 | |
| 100 | struct pca963x_led { |
| 101 | struct pca963x *chip; |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 102 | struct led_classdev led_cdev; |
Peter Meerwald | 3dfedb9 | 2014-07-02 22:50:34 -0700 | [diff] [blame] | 103 | int led_num; /* 0 .. 15 potentially */ |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 104 | u8 gdc; |
| 105 | u8 gfrq; |
| 106 | }; |
| 107 | |
Marek Behún | b5a3b44 | 2020-09-20 02:24:58 +0200 | [diff] [blame] | 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 | |
Marek Behún | 5db8509 | 2020-09-20 02:24:57 +0200 | [diff] [blame] | 116 | static int pca963x_brightness(struct pca963x_led *led, |
Marek Behún | 3911849 | 2020-09-20 02:24:54 +0200 | [diff] [blame] | 117 | enum led_brightness brightness) |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 118 | { |
Marek Behún | 5db8509 | 2020-09-20 02:24:57 +0200 | [diff] [blame] | 119 | struct i2c_client *client = led->chip->client; |
| 120 | struct pca963x_chipdef *chipdef = led->chip->chipdef; |
Marek Behún | 3911849 | 2020-09-20 02:24:54 +0200 | [diff] [blame] | 121 | u8 ledout_addr, ledout, mask, val; |
| 122 | int shift; |
Andrew Lunn | 5029a2e | 2015-08-20 12:38:44 +0200 | [diff] [blame] | 123 | int ret; |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 124 | |
Marek Behún | 5db8509 | 2020-09-20 02:24:57 +0200 | [diff] [blame] | 125 | ledout_addr = chipdef->ledout_base + (led->led_num / 4); |
| 126 | shift = 2 * (led->led_num % 4); |
Marek Behún | 3911849 | 2020-09-20 02:24:54 +0200 | [diff] [blame] | 127 | mask = 0x3 << shift; |
| 128 | ledout = i2c_smbus_read_byte_data(client, ledout_addr); |
| 129 | |
Andrew Lunn | 5029a2e | 2015-08-20 12:38:44 +0200 | [diff] [blame] | 130 | switch (brightness) { |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 131 | case LED_FULL: |
Marek Behún | 3911849 | 2020-09-20 02:24:54 +0200 | [diff] [blame] | 132 | val = (ledout & ~mask) | (PCA963X_LED_ON << shift); |
| 133 | ret = i2c_smbus_write_byte_data(client, ledout_addr, val); |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 134 | break; |
| 135 | case LED_OFF: |
Marek Behún | 3911849 | 2020-09-20 02:24:54 +0200 | [diff] [blame] | 136 | val = ledout & ~mask; |
| 137 | ret = i2c_smbus_write_byte_data(client, ledout_addr, val); |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 138 | break; |
| 139 | default: |
Marek Behún | 3911849 | 2020-09-20 02:24:54 +0200 | [diff] [blame] | 140 | ret = i2c_smbus_write_byte_data(client, |
| 141 | PCA963X_PWM_BASE + |
Marek Behún | 5db8509 | 2020-09-20 02:24:57 +0200 | [diff] [blame] | 142 | led->led_num, |
Marek Behún | 3911849 | 2020-09-20 02:24:54 +0200 | [diff] [blame] | 143 | brightness); |
Andrew Lunn | 5029a2e | 2015-08-20 12:38:44 +0200 | [diff] [blame] | 144 | if (ret < 0) |
Matt Ranostay | a8c170b | 2016-10-28 18:20:42 -0700 | [diff] [blame] | 145 | return ret; |
Marek Behún | 3911849 | 2020-09-20 02:24:54 +0200 | [diff] [blame] | 146 | |
| 147 | val = (ledout & ~mask) | (PCA963X_LED_PWM << shift); |
| 148 | ret = i2c_smbus_write_byte_data(client, ledout_addr, val); |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 149 | break; |
| 150 | } |
Matt Ranostay | a8c170b | 2016-10-28 18:20:42 -0700 | [diff] [blame] | 151 | |
Andrew Lunn | 5029a2e | 2015-08-20 12:38:44 +0200 | [diff] [blame] | 152 | return ret; |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Marek Behún | 5db8509 | 2020-09-20 02:24:57 +0200 | [diff] [blame] | 155 | static void pca963x_blink(struct pca963x_led *led) |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 156 | { |
Marek Behún | 5db8509 | 2020-09-20 02:24:57 +0200 | [diff] [blame] | 157 | struct i2c_client *client = led->chip->client; |
| 158 | struct pca963x_chipdef *chipdef = led->chip->chipdef; |
Marek Behún | 3911849 | 2020-09-20 02:24:54 +0200 | [diff] [blame] | 159 | u8 ledout_addr, ledout, mask, val, mode2; |
| 160 | int shift; |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 161 | |
Marek Behún | 5db8509 | 2020-09-20 02:24:57 +0200 | [diff] [blame] | 162 | ledout_addr = chipdef->ledout_base + (led->led_num / 4); |
| 163 | shift = 2 * (led->led_num % 4); |
Marek Behún | 3911849 | 2020-09-20 02:24:54 +0200 | [diff] [blame] | 164 | mask = 0x3 << shift; |
| 165 | mode2 = i2c_smbus_read_byte_data(client, PCA963X_MODE2); |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 166 | |
Marek Behún | 5db8509 | 2020-09-20 02:24:57 +0200 | [diff] [blame] | 167 | i2c_smbus_write_byte_data(client, chipdef->grppwm, led->gdc); |
Marek Behún | 3911849 | 2020-09-20 02:24:54 +0200 | [diff] [blame] | 168 | |
Marek Behún | 5db8509 | 2020-09-20 02:24:57 +0200 | [diff] [blame] | 169 | i2c_smbus_write_byte_data(client, chipdef->grpfreq, led->gfrq); |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 170 | |
| 171 | if (!(mode2 & PCA963X_MODE2_DMBLNK)) |
Marek Behún | 3911849 | 2020-09-20 02:24:54 +0200 | [diff] [blame] | 172 | i2c_smbus_write_byte_data(client, PCA963X_MODE2, |
| 173 | mode2 | PCA963X_MODE2_DMBLNK); |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 174 | |
Marek Behún | 5db8509 | 2020-09-20 02:24:57 +0200 | [diff] [blame] | 175 | mutex_lock(&led->chip->mutex); |
Marek Behún | 3911849 | 2020-09-20 02:24:54 +0200 | [diff] [blame] | 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 | |
Marek Behún | 5db8509 | 2020-09-20 02:24:57 +0200 | [diff] [blame] | 183 | mutex_unlock(&led->chip->mutex); |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Marek Behún | 5db8509 | 2020-09-20 02:24:57 +0200 | [diff] [blame] | 186 | static int pca963x_power_state(struct pca963x_led *led) |
Matt Ranostay | a8c170b | 2016-10-28 18:20:42 -0700 | [diff] [blame] | 187 | { |
Marek Behún | 5db8509 | 2020-09-20 02:24:57 +0200 | [diff] [blame] | 188 | struct i2c_client *client = led->chip->client; |
| 189 | unsigned long *leds_on = &led->chip->leds_on; |
Marek Behún | 3911849 | 2020-09-20 02:24:54 +0200 | [diff] [blame] | 190 | unsigned long cached_leds = *leds_on; |
Matt Ranostay | a8c170b | 2016-10-28 18:20:42 -0700 | [diff] [blame] | 191 | |
Marek Behún | 5db8509 | 2020-09-20 02:24:57 +0200 | [diff] [blame] | 192 | if (led->led_cdev.brightness) |
| 193 | set_bit(led->led_num, leds_on); |
Matt Ranostay | a8c170b | 2016-10-28 18:20:42 -0700 | [diff] [blame] | 194 | else |
Marek Behún | 5db8509 | 2020-09-20 02:24:57 +0200 | [diff] [blame] | 195 | clear_bit(led->led_num, leds_on); |
Matt Ranostay | a8c170b | 2016-10-28 18:20:42 -0700 | [diff] [blame] | 196 | |
| 197 | if (!(*leds_on) != !cached_leds) |
Marek Behún | 3911849 | 2020-09-20 02:24:54 +0200 | [diff] [blame] | 198 | return i2c_smbus_write_byte_data(client, PCA963X_MODE1, |
| 199 | *leds_on ? 0 : BIT(4)); |
Matt Ranostay | a8c170b | 2016-10-28 18:20:42 -0700 | [diff] [blame] | 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
Andrew Lunn | 5029a2e | 2015-08-20 12:38:44 +0200 | [diff] [blame] | 204 | static int pca963x_led_set(struct led_classdev *led_cdev, |
Marek Behún | 3911849 | 2020-09-20 02:24:54 +0200 | [diff] [blame] | 205 | enum led_brightness value) |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 206 | { |
Marek Behún | 5db8509 | 2020-09-20 02:24:57 +0200 | [diff] [blame] | 207 | struct pca963x_led *led; |
Matt Ranostay | a8c170b | 2016-10-28 18:20:42 -0700 | [diff] [blame] | 208 | int ret; |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 209 | |
Marek Behún | 5db8509 | 2020-09-20 02:24:57 +0200 | [diff] [blame] | 210 | led = container_of(led_cdev, struct pca963x_led, led_cdev); |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 211 | |
Marek Behún | 5db8509 | 2020-09-20 02:24:57 +0200 | [diff] [blame] | 212 | mutex_lock(&led->chip->mutex); |
Matt Ranostay | a8c170b | 2016-10-28 18:20:42 -0700 | [diff] [blame] | 213 | |
Marek Behún | 5db8509 | 2020-09-20 02:24:57 +0200 | [diff] [blame] | 214 | ret = pca963x_brightness(led, value); |
Matt Ranostay | a8c170b | 2016-10-28 18:20:42 -0700 | [diff] [blame] | 215 | if (ret < 0) |
| 216 | goto unlock; |
Marek Behún | 5db8509 | 2020-09-20 02:24:57 +0200 | [diff] [blame] | 217 | ret = pca963x_power_state(led); |
Matt Ranostay | a8c170b | 2016-10-28 18:20:42 -0700 | [diff] [blame] | 218 | |
| 219 | unlock: |
Marek Behún | 5db8509 | 2020-09-20 02:24:57 +0200 | [diff] [blame] | 220 | mutex_unlock(&led->chip->mutex); |
Matt Ranostay | a8c170b | 2016-10-28 18:20:42 -0700 | [diff] [blame] | 221 | return ret; |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Marek Behún | 5db8509 | 2020-09-20 02:24:57 +0200 | [diff] [blame] | 224 | static unsigned int pca963x_period_scale(struct pca963x_led *led, |
Marek Behún | 3911849 | 2020-09-20 02:24:54 +0200 | [diff] [blame] | 225 | unsigned int val) |
Matt Ranostay | 35c7d30 | 2016-10-13 06:16:12 -0700 | [diff] [blame] | 226 | { |
Marek Behún | 5db8509 | 2020-09-20 02:24:57 +0200 | [diff] [blame] | 227 | unsigned int scaling = led->chip->chipdef->scaling; |
Matt Ranostay | 35c7d30 | 2016-10-13 06:16:12 -0700 | [diff] [blame] | 228 | |
| 229 | return scaling ? DIV_ROUND_CLOSEST(val * scaling, 1000) : val; |
| 230 | } |
| 231 | |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 232 | static int pca963x_blink_set(struct led_classdev *led_cdev, |
Marek Behún | 3911849 | 2020-09-20 02:24:54 +0200 | [diff] [blame] | 233 | unsigned long *delay_on, unsigned long *delay_off) |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 234 | { |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 235 | unsigned long time_on, time_off, period; |
Marek Behún | 5db8509 | 2020-09-20 02:24:57 +0200 | [diff] [blame] | 236 | struct pca963x_led *led; |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 237 | u8 gdc, gfrq; |
| 238 | |
Marek Behún | 5db8509 | 2020-09-20 02:24:57 +0200 | [diff] [blame] | 239 | led = container_of(led_cdev, struct pca963x_led, led_cdev); |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 240 | |
| 241 | time_on = *delay_on; |
| 242 | time_off = *delay_off; |
| 243 | |
| 244 | /* If both zero, pick reasonable defaults of 500ms each */ |
| 245 | if (!time_on && !time_off) { |
| 246 | time_on = 500; |
| 247 | time_off = 500; |
| 248 | } |
| 249 | |
Marek Behún | 5db8509 | 2020-09-20 02:24:57 +0200 | [diff] [blame] | 250 | period = pca963x_period_scale(led, time_on + time_off); |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 251 | |
| 252 | /* If period not supported by hardware, default to someting sane. */ |
| 253 | if ((period < PCA963X_BLINK_PERIOD_MIN) || |
| 254 | (period > PCA963X_BLINK_PERIOD_MAX)) { |
| 255 | time_on = 500; |
| 256 | time_off = 500; |
Marek Behún | 5db8509 | 2020-09-20 02:24:57 +0200 | [diff] [blame] | 257 | period = pca963x_period_scale(led, 1000); |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | /* |
| 261 | * From manual: duty cycle = (GDC / 256) -> |
| 262 | * (time_on / period) = (GDC / 256) -> |
| 263 | * GDC = ((time_on * 256) / period) |
| 264 | */ |
Marek Behún | 5db8509 | 2020-09-20 02:24:57 +0200 | [diff] [blame] | 265 | gdc = (pca963x_period_scale(led, time_on) * 256) / period; |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 266 | |
| 267 | /* |
| 268 | * From manual: period = ((GFRQ + 1) / 24) in seconds. |
| 269 | * So, period (in ms) = (((GFRQ + 1) / 24) * 1000) -> |
| 270 | * GFRQ = ((period * 24 / 1000) - 1) |
| 271 | */ |
| 272 | gfrq = (period * 24 / 1000) - 1; |
| 273 | |
Marek Behún | 5db8509 | 2020-09-20 02:24:57 +0200 | [diff] [blame] | 274 | led->gdc = gdc; |
| 275 | led->gfrq = gfrq; |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 276 | |
Marek Behún | 5db8509 | 2020-09-20 02:24:57 +0200 | [diff] [blame] | 277 | pca963x_blink(led); |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 278 | |
| 279 | *delay_on = time_on; |
| 280 | *delay_off = time_off; |
| 281 | |
| 282 | return 0; |
| 283 | } |
| 284 | |
Marek Behún | 85fc8ef | 2020-09-20 02:24:59 +0200 | [diff] [blame] | 285 | static int pca963x_register_leds(struct i2c_client *client, |
| 286 | struct pca963x *chip) |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 287 | { |
Marek Behún | 85fc8ef | 2020-09-20 02:24:59 +0200 | [diff] [blame] | 288 | struct pca963x_chipdef *chipdef = chip->chipdef; |
| 289 | struct pca963x_led *led = chip->leds; |
| 290 | struct device *dev = &client->dev; |
Andy Shevchenko | 0b6034d | 2019-03-25 16:02:07 +0200 | [diff] [blame] | 291 | struct fwnode_handle *child; |
Marek Behún | 85fc8ef | 2020-09-20 02:24:59 +0200 | [diff] [blame] | 292 | bool hw_blink; |
| 293 | s32 mode2; |
| 294 | u32 reg; |
| 295 | int ret; |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 296 | |
Marek Behún | 3911849 | 2020-09-20 02:24:54 +0200 | [diff] [blame] | 297 | if (device_property_read_u32(dev, "nxp,period-scale", |
Marek Behún | fc0b1ec | 2020-09-20 02:24:56 +0200 | [diff] [blame] | 298 | &chipdef->scaling)) |
| 299 | chipdef->scaling = 1000; |
Matt Ranostay | 35c7d30 | 2016-10-13 06:16:12 -0700 | [diff] [blame] | 300 | |
Marek Behún | 85fc8ef | 2020-09-20 02:24:59 +0200 | [diff] [blame] | 301 | hw_blink = device_property_read_bool(dev, "nxp,hw-blink"); |
| 302 | |
| 303 | mode2 = i2c_smbus_read_byte_data(client, PCA963X_MODE2); |
| 304 | if (mode2 < 0) |
| 305 | return mode2; |
| 306 | |
| 307 | /* default to open-drain unless totem pole (push-pull) is specified */ |
| 308 | if (device_property_read_bool(dev, "nxp,totem-pole")) |
| 309 | mode2 |= PCA963X_MODE2_OUTDRV; |
| 310 | else |
| 311 | mode2 &= ~PCA963X_MODE2_OUTDRV; |
| 312 | |
Anders Darander | bb29b9c | 2017-04-27 08:37:33 +0200 | [diff] [blame] | 313 | /* default to non-inverted output, unless inverted is specified */ |
Marek Behún | 3911849 | 2020-09-20 02:24:54 +0200 | [diff] [blame] | 314 | if (device_property_read_bool(dev, "nxp,inverted-out")) |
Marek Behún | 85fc8ef | 2020-09-20 02:24:59 +0200 | [diff] [blame] | 315 | mode2 |= PCA963X_MODE2_INVRT; |
Anders Darander | bb29b9c | 2017-04-27 08:37:33 +0200 | [diff] [blame] | 316 | else |
Marek Behún | 85fc8ef | 2020-09-20 02:24:59 +0200 | [diff] [blame] | 317 | mode2 &= ~PCA963X_MODE2_INVRT; |
Anders Darander | bb29b9c | 2017-04-27 08:37:33 +0200 | [diff] [blame] | 318 | |
Marek Behún | 85fc8ef | 2020-09-20 02:24:59 +0200 | [diff] [blame] | 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) { |
Marek Behún | 564ead1 | 2020-09-20 02:25:00 +0200 | [diff] [blame] | 324 | struct led_init_data init_data = {}; |
| 325 | char default_label[32]; |
| 326 | |
Marek Behún | 85fc8ef | 2020-09-20 02:24:59 +0200 | [diff] [blame] | 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 | |
Marek Behún | 85fc8ef | 2020-09-20 02:24:59 +0200 | [diff] [blame] | 335 | led->led_num = reg; |
| 336 | led->chip = chip; |
Marek Behún | 85fc8ef | 2020-09-20 02:24:59 +0200 | [diff] [blame] | 337 | led->led_cdev.brightness_set_blocking = pca963x_led_set; |
| 338 | if (hw_blink) |
| 339 | led->led_cdev.blink_set = pca963x_blink_set; |
| 340 | |
Marek Behún | 564ead1 | 2020-09-20 02:25:00 +0200 | [diff] [blame] | 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); |
Marek Behún | 85fc8ef | 2020-09-20 02:24:59 +0200 | [diff] [blame] | 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; |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | static const struct of_device_id of_pca963x_match[] = { |
| 366 | { .compatible = "nxp,pca9632", }, |
| 367 | { .compatible = "nxp,pca9633", }, |
| 368 | { .compatible = "nxp,pca9634", }, |
Peter Meerwald | 3dfedb9 | 2014-07-02 22:50:34 -0700 | [diff] [blame] | 369 | { .compatible = "nxp,pca9635", }, |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 370 | {}, |
| 371 | }; |
Javier Martinez Canillas | 4d59ed8 | 2015-08-25 08:31:16 +0200 | [diff] [blame] | 372 | MODULE_DEVICE_TABLE(of, of_pca963x_match); |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 373 | |
| 374 | static int pca963x_probe(struct i2c_client *client, |
Marek Behún | 3911849 | 2020-09-20 02:24:54 +0200 | [diff] [blame] | 375 | const struct i2c_device_id *id) |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 376 | { |
Marek Behún | 3911849 | 2020-09-20 02:24:54 +0200 | [diff] [blame] | 377 | struct device *dev = &client->dev; |
Marek Behún | fc0b1ec | 2020-09-20 02:24:56 +0200 | [diff] [blame] | 378 | struct pca963x_chipdef *chipdef; |
Marek Behún | fc0b1ec | 2020-09-20 02:24:56 +0200 | [diff] [blame] | 379 | struct pca963x *chip; |
Marek Behún | 85fc8ef | 2020-09-20 02:24:59 +0200 | [diff] [blame] | 380 | int i, count; |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 381 | |
Marek Behún | fc0b1ec | 2020-09-20 02:24:56 +0200 | [diff] [blame] | 382 | chipdef = &pca963x_chipdefs[id->driver_data]; |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 383 | |
Marek Behún | 85fc8ef | 2020-09-20 02:24:59 +0200 | [diff] [blame] | 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); |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 388 | return -EINVAL; |
| 389 | } |
| 390 | |
Marek Behún | 85fc8ef | 2020-09-20 02:24:59 +0200 | [diff] [blame] | 391 | chip = devm_kzalloc(dev, struct_size(chip, leds, count), GFP_KERNEL); |
Marek Behún | fc0b1ec | 2020-09-20 02:24:56 +0200 | [diff] [blame] | 392 | if (!chip) |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 393 | return -ENOMEM; |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 394 | |
Marek Behún | fc0b1ec | 2020-09-20 02:24:56 +0200 | [diff] [blame] | 395 | i2c_set_clientdata(client, chip); |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 396 | |
Marek Behún | fc0b1ec | 2020-09-20 02:24:56 +0200 | [diff] [blame] | 397 | mutex_init(&chip->mutex); |
| 398 | chip->chipdef = chipdef; |
| 399 | chip->client = client; |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 400 | |
| 401 | /* Turn off LEDs by default*/ |
Marek Behún | fc0b1ec | 2020-09-20 02:24:56 +0200 | [diff] [blame] | 402 | for (i = 0; i < chipdef->n_leds / 4; i++) |
| 403 | i2c_smbus_write_byte_data(client, chipdef->ledout_base + i, 0x00); |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 404 | |
Matt Ranostay | a8c170b | 2016-10-28 18:20:42 -0700 | [diff] [blame] | 405 | /* Disable LED all-call address, and power down initially */ |
| 406 | i2c_smbus_write_byte_data(client, PCA963X_MODE1, BIT(4)); |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 407 | |
Marek Behún | 85fc8ef | 2020-09-20 02:24:59 +0200 | [diff] [blame] | 408 | return pca963x_register_leds(client, chip); |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | static struct i2c_driver pca963x_driver = { |
| 412 | .driver = { |
| 413 | .name = "leds-pca963x", |
Andy Shevchenko | 0b6034d | 2019-03-25 16:02:07 +0200 | [diff] [blame] | 414 | .of_match_table = of_pca963x_match, |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 415 | }, |
| 416 | .probe = pca963x_probe, |
Ricardo Ribalda Delgado | 56a1740 | 2013-08-14 14:23:50 -0700 | [diff] [blame] | 417 | .id_table = pca963x_id, |
| 418 | }; |
| 419 | |
| 420 | module_i2c_driver(pca963x_driver); |
| 421 | |
| 422 | MODULE_AUTHOR("Peter Meerwald <p.meerwald@bct-electronic.com>"); |
| 423 | MODULE_DESCRIPTION("PCA963X LED driver"); |
| 424 | MODULE_LICENSE("GPL v2"); |