Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Milo(Woogyom) Kim | c93d08f | 2013-02-05 18:01:23 +0900 | [diff] [blame] | 2 | /* |
| 3 | * LP55XX Common Driver Header |
| 4 | * |
| 5 | * Copyright (C) 2012 Texas Instruments |
| 6 | * |
| 7 | * Author: Milo(Woogyom) Kim <milo.kim@ti.com> |
| 8 | * |
Milo(Woogyom) Kim | c93d08f | 2013-02-05 18:01:23 +0900 | [diff] [blame] | 9 | * Derived from leds-lp5521.c, leds-lp5523.c |
| 10 | */ |
| 11 | |
| 12 | #ifndef _LEDS_LP55XX_COMMON_H |
| 13 | #define _LEDS_LP55XX_COMMON_H |
| 14 | |
Dan Murphy | 92a8156 | 2020-07-16 13:20:01 -0500 | [diff] [blame] | 15 | #include <linux/led-class-multicolor.h> |
| 16 | |
Milo(Woogyom) Kim | 10c06d1 | 2013-02-05 19:17:20 +0900 | [diff] [blame] | 17 | enum lp55xx_engine_index { |
| 18 | LP55XX_ENGINE_INVALID, |
| 19 | LP55XX_ENGINE_1, |
| 20 | LP55XX_ENGINE_2, |
| 21 | LP55XX_ENGINE_3, |
Milo Kim | 6841a91 | 2013-08-08 12:45:41 +0900 | [diff] [blame] | 22 | LP55XX_ENGINE_MAX = LP55XX_ENGINE_3, |
| 23 | }; |
| 24 | |
| 25 | enum lp55xx_engine_mode { |
| 26 | LP55XX_ENGINE_DISABLED, |
| 27 | LP55XX_ENGINE_LOAD, |
| 28 | LP55XX_ENGINE_RUN, |
Milo(Woogyom) Kim | 10c06d1 | 2013-02-05 19:17:20 +0900 | [diff] [blame] | 29 | }; |
| 30 | |
Milo Kim | 3603097 | 2013-08-08 12:46:43 +0900 | [diff] [blame] | 31 | #define LP55XX_DEV_ATTR_RW(name, show, store) \ |
| 32 | DEVICE_ATTR(name, S_IRUGO | S_IWUSR, show, store) |
| 33 | #define LP55XX_DEV_ATTR_RO(name, show) \ |
| 34 | DEVICE_ATTR(name, S_IRUGO, show, NULL) |
| 35 | #define LP55XX_DEV_ATTR_WO(name, store) \ |
| 36 | DEVICE_ATTR(name, S_IWUSR, NULL, store) |
| 37 | |
| 38 | #define show_mode(nr) \ |
| 39 | static ssize_t show_engine##nr##_mode(struct device *dev, \ |
| 40 | struct device_attribute *attr, \ |
| 41 | char *buf) \ |
| 42 | { \ |
| 43 | return show_engine_mode(dev, attr, buf, nr); \ |
| 44 | } |
| 45 | |
| 46 | #define store_mode(nr) \ |
| 47 | static ssize_t store_engine##nr##_mode(struct device *dev, \ |
| 48 | struct device_attribute *attr, \ |
| 49 | const char *buf, size_t len) \ |
| 50 | { \ |
| 51 | return store_engine_mode(dev, attr, buf, len, nr); \ |
| 52 | } |
| 53 | |
| 54 | #define show_leds(nr) \ |
| 55 | static ssize_t show_engine##nr##_leds(struct device *dev, \ |
| 56 | struct device_attribute *attr, \ |
| 57 | char *buf) \ |
| 58 | { \ |
| 59 | return show_engine_leds(dev, attr, buf, nr); \ |
| 60 | } |
| 61 | |
| 62 | #define store_leds(nr) \ |
| 63 | static ssize_t store_engine##nr##_leds(struct device *dev, \ |
| 64 | struct device_attribute *attr, \ |
| 65 | const char *buf, size_t len) \ |
| 66 | { \ |
| 67 | return store_engine_leds(dev, attr, buf, len, nr); \ |
| 68 | } |
| 69 | |
| 70 | #define store_load(nr) \ |
| 71 | static ssize_t store_engine##nr##_load(struct device *dev, \ |
| 72 | struct device_attribute *attr, \ |
| 73 | const char *buf, size_t len) \ |
| 74 | { \ |
| 75 | return store_engine_load(dev, attr, buf, len, nr); \ |
| 76 | } |
| 77 | |
Milo(Woogyom) Kim | c93d08f | 2013-02-05 18:01:23 +0900 | [diff] [blame] | 78 | struct lp55xx_led; |
| 79 | struct lp55xx_chip; |
| 80 | |
| 81 | /* |
Milo(Woogyom) Kim | 48068d5 | 2013-02-05 18:08:49 +0900 | [diff] [blame] | 82 | * struct lp55xx_reg |
| 83 | * @addr : Register address |
| 84 | * @val : Register value |
| 85 | */ |
| 86 | struct lp55xx_reg { |
| 87 | u8 addr; |
| 88 | u8 val; |
| 89 | }; |
| 90 | |
| 91 | /* |
| 92 | * struct lp55xx_device_config |
| 93 | * @reset : Chip specific reset command |
Milo(Woogyom) Kim | e3a700d | 2013-02-05 18:09:56 +0900 | [diff] [blame] | 94 | * @enable : Chip specific enable command |
Milo(Woogyom) Kim | 0e20234 | 2013-02-05 19:07:34 +0900 | [diff] [blame] | 95 | * @max_channel : Maximum number of channels |
Milo(Woogyom) Kim | ffbdccd | 2013-02-05 18:57:36 +0900 | [diff] [blame] | 96 | * @post_init_device : Chip specific initialization code |
Andrew Lunn | 95b2af6 | 2015-08-20 12:22:57 +0200 | [diff] [blame] | 97 | * @brightness_fn : Brightness function |
Dan Murphy | 92a8156 | 2020-07-16 13:20:01 -0500 | [diff] [blame] | 98 | * @multicolor_brightness_fn : Multicolor brightness function |
Milo(Woogyom) Kim | 9e9b3db | 2013-02-05 19:06:27 +0900 | [diff] [blame] | 99 | * @set_led_current : LED current set function |
Milo(Woogyom) Kim | 10c06d1 | 2013-02-05 19:17:20 +0900 | [diff] [blame] | 100 | * @firmware_cb : Call function when the firmware is loaded |
| 101 | * @run_engine : Run internal engine for pattern |
Milo(Woogyom) Kim | 240085e | 2013-02-05 19:20:01 +0900 | [diff] [blame] | 102 | * @dev_attr_group : Device specific attributes |
Milo(Woogyom) Kim | 48068d5 | 2013-02-05 18:08:49 +0900 | [diff] [blame] | 103 | */ |
| 104 | struct lp55xx_device_config { |
| 105 | const struct lp55xx_reg reset; |
Milo(Woogyom) Kim | e3a700d | 2013-02-05 18:09:56 +0900 | [diff] [blame] | 106 | const struct lp55xx_reg enable; |
Milo(Woogyom) Kim | 0e20234 | 2013-02-05 19:07:34 +0900 | [diff] [blame] | 107 | const int max_channel; |
Milo(Woogyom) Kim | ffbdccd | 2013-02-05 18:57:36 +0900 | [diff] [blame] | 108 | |
| 109 | /* define if the device has specific initialization process */ |
| 110 | int (*post_init_device) (struct lp55xx_chip *chip); |
Milo(Woogyom) Kim | 9e9b3db | 2013-02-05 19:06:27 +0900 | [diff] [blame] | 111 | |
Dan Murphy | 92a8156 | 2020-07-16 13:20:01 -0500 | [diff] [blame] | 112 | /* set LED brightness */ |
Andrew Lunn | 95b2af6 | 2015-08-20 12:22:57 +0200 | [diff] [blame] | 113 | int (*brightness_fn)(struct lp55xx_led *led); |
Milo(Woogyom) Kim | 9e9b3db | 2013-02-05 19:06:27 +0900 | [diff] [blame] | 114 | |
Dan Murphy | 92a8156 | 2020-07-16 13:20:01 -0500 | [diff] [blame] | 115 | /* set multicolor LED brightness */ |
| 116 | int (*multicolor_brightness_fn)(struct lp55xx_led *led); |
| 117 | |
Milo(Woogyom) Kim | 9e9b3db | 2013-02-05 19:06:27 +0900 | [diff] [blame] | 118 | /* current setting function */ |
| 119 | void (*set_led_current) (struct lp55xx_led *led, u8 led_current); |
Milo(Woogyom) Kim | 10c06d1 | 2013-02-05 19:17:20 +0900 | [diff] [blame] | 120 | |
| 121 | /* access program memory when the firmware is loaded */ |
| 122 | void (*firmware_cb)(struct lp55xx_chip *chip); |
| 123 | |
| 124 | /* used for running firmware LED patterns */ |
| 125 | void (*run_engine) (struct lp55xx_chip *chip, bool start); |
Milo(Woogyom) Kim | 240085e | 2013-02-05 19:20:01 +0900 | [diff] [blame] | 126 | |
| 127 | /* additional device specific attributes */ |
| 128 | const struct attribute_group *dev_attr_group; |
Milo(Woogyom) Kim | 48068d5 | 2013-02-05 18:08:49 +0900 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | /* |
Milo Kim | 6841a91 | 2013-08-08 12:45:41 +0900 | [diff] [blame] | 132 | * struct lp55xx_engine |
| 133 | * @mode : Engine mode |
| 134 | * @led_mux : Mux bits for LED selection. Only used in LP5523 |
| 135 | */ |
| 136 | struct lp55xx_engine { |
| 137 | enum lp55xx_engine_mode mode; |
| 138 | u16 led_mux; |
| 139 | }; |
| 140 | |
| 141 | /* |
Milo(Woogyom) Kim | c93d08f | 2013-02-05 18:01:23 +0900 | [diff] [blame] | 142 | * struct lp55xx_chip |
| 143 | * @cl : I2C communication for access registers |
| 144 | * @pdata : Platform specific data |
| 145 | * @lock : Lock for user-space interface |
| 146 | * @num_leds : Number of registered LEDs |
Milo(Woogyom) Kim | 48068d5 | 2013-02-05 18:08:49 +0900 | [diff] [blame] | 147 | * @cfg : Device specific configuration data |
Milo(Woogyom) Kim | 10c06d1 | 2013-02-05 19:17:20 +0900 | [diff] [blame] | 148 | * @engine_idx : Selected engine number |
Milo Kim | 6841a91 | 2013-08-08 12:45:41 +0900 | [diff] [blame] | 149 | * @engines : Engine structure for the device attribute R/W interface |
Milo(Woogyom) Kim | 10c06d1 | 2013-02-05 19:17:20 +0900 | [diff] [blame] | 150 | * @fw : Firmware data for running a LED pattern |
Milo(Woogyom) Kim | c93d08f | 2013-02-05 18:01:23 +0900 | [diff] [blame] | 151 | */ |
| 152 | struct lp55xx_chip { |
| 153 | struct i2c_client *cl; |
Kim, Milo | 53b4192 | 2013-03-20 17:37:00 -0700 | [diff] [blame] | 154 | struct clk *clk; |
Milo(Woogyom) Kim | c93d08f | 2013-02-05 18:01:23 +0900 | [diff] [blame] | 155 | struct lp55xx_platform_data *pdata; |
| 156 | struct mutex lock; /* lock for user-space interface */ |
| 157 | int num_leds; |
Milo(Woogyom) Kim | 48068d5 | 2013-02-05 18:08:49 +0900 | [diff] [blame] | 158 | struct lp55xx_device_config *cfg; |
Milo(Woogyom) Kim | 10c06d1 | 2013-02-05 19:17:20 +0900 | [diff] [blame] | 159 | enum lp55xx_engine_index engine_idx; |
Milo Kim | 6841a91 | 2013-08-08 12:45:41 +0900 | [diff] [blame] | 160 | struct lp55xx_engine engines[LP55XX_ENGINE_MAX]; |
Milo(Woogyom) Kim | 10c06d1 | 2013-02-05 19:17:20 +0900 | [diff] [blame] | 161 | const struct firmware *fw; |
Milo(Woogyom) Kim | c93d08f | 2013-02-05 18:01:23 +0900 | [diff] [blame] | 162 | }; |
| 163 | |
| 164 | /* |
| 165 | * struct lp55xx_led |
| 166 | * @chan_nr : Channel number |
| 167 | * @cdev : LED class device |
Dan Murphy | 92a8156 | 2020-07-16 13:20:01 -0500 | [diff] [blame] | 168 | * @mc_cdev : Multi color class device |
| 169 | * @color_components: Multi color LED map information |
Milo(Woogyom) Kim | c93d08f | 2013-02-05 18:01:23 +0900 | [diff] [blame] | 170 | * @led_current : Current setting at each led channel |
| 171 | * @max_current : Maximun current at each led channel |
Milo(Woogyom) Kim | c93d08f | 2013-02-05 18:01:23 +0900 | [diff] [blame] | 172 | * @brightness : Brightness value |
| 173 | * @chip : The lp55xx chip data |
| 174 | */ |
| 175 | struct lp55xx_led { |
| 176 | int chan_nr; |
| 177 | struct led_classdev cdev; |
Dan Murphy | 92a8156 | 2020-07-16 13:20:01 -0500 | [diff] [blame] | 178 | struct led_classdev_mc mc_cdev; |
Milo(Woogyom) Kim | c93d08f | 2013-02-05 18:01:23 +0900 | [diff] [blame] | 179 | u8 led_current; |
| 180 | u8 max_current; |
Milo(Woogyom) Kim | c93d08f | 2013-02-05 18:01:23 +0900 | [diff] [blame] | 181 | u8 brightness; |
| 182 | struct lp55xx_chip *chip; |
| 183 | }; |
| 184 | |
| 185 | /* register access */ |
| 186 | extern int lp55xx_write(struct lp55xx_chip *chip, u8 reg, u8 val); |
| 187 | extern int lp55xx_read(struct lp55xx_chip *chip, u8 reg, u8 *val); |
| 188 | extern int lp55xx_update_bits(struct lp55xx_chip *chip, u8 reg, |
| 189 | u8 mask, u8 val); |
| 190 | |
Kim, Milo | 53b4192 | 2013-03-20 17:37:00 -0700 | [diff] [blame] | 191 | /* external clock detection */ |
| 192 | extern bool lp55xx_is_extclk_used(struct lp55xx_chip *chip); |
| 193 | |
Milo(Woogyom) Kim | 6ce6176 | 2013-02-05 19:03:02 +0900 | [diff] [blame] | 194 | /* common device init/deinit functions */ |
Milo(Woogyom) Kim | a85908d | 2013-02-05 18:07:20 +0900 | [diff] [blame] | 195 | extern int lp55xx_init_device(struct lp55xx_chip *chip); |
Milo(Woogyom) Kim | 6ce6176 | 2013-02-05 19:03:02 +0900 | [diff] [blame] | 196 | extern void lp55xx_deinit_device(struct lp55xx_chip *chip); |
Milo(Woogyom) Kim | a85908d | 2013-02-05 18:07:20 +0900 | [diff] [blame] | 197 | |
Milo(Woogyom) Kim | 9e9b3db | 2013-02-05 19:06:27 +0900 | [diff] [blame] | 198 | /* common LED class device functions */ |
| 199 | extern int lp55xx_register_leds(struct lp55xx_led *led, |
| 200 | struct lp55xx_chip *chip); |
Milo(Woogyom) Kim | b3b6f81 | 2013-02-05 19:15:27 +0900 | [diff] [blame] | 201 | |
| 202 | /* common device attributes functions */ |
| 203 | extern int lp55xx_register_sysfs(struct lp55xx_chip *chip); |
Milo(Woogyom) Kim | ba6fa84 | 2013-02-05 19:23:04 +0900 | [diff] [blame] | 204 | extern void lp55xx_unregister_sysfs(struct lp55xx_chip *chip); |
Milo(Woogyom) Kim | b3b6f81 | 2013-02-05 19:15:27 +0900 | [diff] [blame] | 205 | |
Linus Walleij | 7542a04b | 2013-04-23 04:52:59 -0700 | [diff] [blame] | 206 | /* common device tree population function */ |
Milo Kim | ed133352 | 2015-08-24 16:09:55 +0900 | [diff] [blame] | 207 | extern struct lp55xx_platform_data |
Dan Murphy | 92a8156 | 2020-07-16 13:20:01 -0500 | [diff] [blame] | 208 | *lp55xx_of_populate_pdata(struct device *dev, struct device_node *np, |
| 209 | struct lp55xx_chip *chip); |
Linus Walleij | 7542a04b | 2013-04-23 04:52:59 -0700 | [diff] [blame] | 210 | |
Milo(Woogyom) Kim | c93d08f | 2013-02-05 18:01:23 +0900 | [diff] [blame] | 211 | #endif /* _LEDS_LP55XX_COMMON_H */ |