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