Johan Hovold | 16c5c02 | 2012-05-03 12:26:36 +0200 | [diff] [blame] | 1 | /* |
| 2 | * lm3533-core.c -- LM3533 Core |
| 3 | * |
| 4 | * Copyright (C) 2011-2012 Texas Instruments |
| 5 | * |
| 6 | * Author: Johan Hovold <jhovold@gmail.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License as published by the |
| 10 | * Free Software Foundation; either version 2 of the License, or (at your |
| 11 | * option) any later version. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/err.h> |
| 18 | #include <linux/gpio.h> |
| 19 | #include <linux/i2c.h> |
| 20 | #include <linux/mfd/core.h> |
| 21 | #include <linux/regmap.h> |
| 22 | #include <linux/seq_file.h> |
| 23 | #include <linux/slab.h> |
| 24 | #include <linux/uaccess.h> |
| 25 | |
| 26 | #include <linux/mfd/lm3533.h> |
| 27 | |
| 28 | |
| 29 | #define LM3533_BOOST_OVP_MAX 0x03 |
| 30 | #define LM3533_BOOST_OVP_MASK 0x06 |
| 31 | #define LM3533_BOOST_OVP_SHIFT 1 |
| 32 | |
| 33 | #define LM3533_BOOST_FREQ_MAX 0x01 |
| 34 | #define LM3533_BOOST_FREQ_MASK 0x01 |
| 35 | #define LM3533_BOOST_FREQ_SHIFT 0 |
| 36 | |
| 37 | #define LM3533_BL_ID_MASK 1 |
| 38 | #define LM3533_LED_ID_MASK 3 |
| 39 | #define LM3533_BL_ID_MAX 1 |
| 40 | #define LM3533_LED_ID_MAX 3 |
| 41 | |
| 42 | #define LM3533_HVLED_ID_MAX 2 |
| 43 | #define LM3533_LVLED_ID_MAX 5 |
| 44 | |
| 45 | #define LM3533_REG_OUTPUT_CONF1 0x10 |
| 46 | #define LM3533_REG_OUTPUT_CONF2 0x11 |
| 47 | #define LM3533_REG_BOOST_PWM 0x2c |
| 48 | |
| 49 | #define LM3533_REG_MAX 0xb2 |
| 50 | |
| 51 | |
| 52 | static struct mfd_cell lm3533_als_devs[] = { |
| 53 | { |
| 54 | .name = "lm3533-als", |
| 55 | .id = -1, |
| 56 | }, |
| 57 | }; |
| 58 | |
| 59 | static struct mfd_cell lm3533_bl_devs[] = { |
| 60 | { |
| 61 | .name = "lm3533-backlight", |
| 62 | .id = 0, |
| 63 | }, |
| 64 | { |
| 65 | .name = "lm3533-backlight", |
| 66 | .id = 1, |
| 67 | }, |
| 68 | }; |
| 69 | |
| 70 | static struct mfd_cell lm3533_led_devs[] = { |
| 71 | { |
| 72 | .name = "lm3533-leds", |
| 73 | .id = 0, |
| 74 | }, |
| 75 | { |
| 76 | .name = "lm3533-leds", |
| 77 | .id = 1, |
| 78 | }, |
| 79 | { |
| 80 | .name = "lm3533-leds", |
| 81 | .id = 2, |
| 82 | }, |
| 83 | { |
| 84 | .name = "lm3533-leds", |
| 85 | .id = 3, |
| 86 | }, |
| 87 | }; |
| 88 | |
| 89 | int lm3533_read(struct lm3533 *lm3533, u8 reg, u8 *val) |
| 90 | { |
| 91 | int tmp; |
| 92 | int ret; |
| 93 | |
| 94 | ret = regmap_read(lm3533->regmap, reg, &tmp); |
| 95 | if (ret < 0) { |
| 96 | dev_err(lm3533->dev, "failed to read register %02x: %d\n", |
| 97 | reg, ret); |
| 98 | return ret; |
| 99 | } |
| 100 | |
| 101 | *val = tmp; |
| 102 | |
| 103 | dev_dbg(lm3533->dev, "read [%02x]: %02x\n", reg, *val); |
| 104 | |
| 105 | return ret; |
| 106 | } |
| 107 | EXPORT_SYMBOL_GPL(lm3533_read); |
| 108 | |
| 109 | int lm3533_write(struct lm3533 *lm3533, u8 reg, u8 val) |
| 110 | { |
| 111 | int ret; |
| 112 | |
| 113 | dev_dbg(lm3533->dev, "write [%02x]: %02x\n", reg, val); |
| 114 | |
| 115 | ret = regmap_write(lm3533->regmap, reg, val); |
| 116 | if (ret < 0) { |
| 117 | dev_err(lm3533->dev, "failed to write register %02x: %d\n", |
| 118 | reg, ret); |
| 119 | } |
| 120 | |
| 121 | return ret; |
| 122 | } |
| 123 | EXPORT_SYMBOL_GPL(lm3533_write); |
| 124 | |
| 125 | int lm3533_update(struct lm3533 *lm3533, u8 reg, u8 val, u8 mask) |
| 126 | { |
| 127 | int ret; |
| 128 | |
| 129 | dev_dbg(lm3533->dev, "update [%02x]: %02x/%02x\n", reg, val, mask); |
| 130 | |
| 131 | ret = regmap_update_bits(lm3533->regmap, reg, val, mask); |
| 132 | if (ret < 0) { |
| 133 | dev_err(lm3533->dev, "failed to update register %02x: %d\n", |
| 134 | reg, ret); |
| 135 | } |
| 136 | |
| 137 | return ret; |
| 138 | } |
| 139 | EXPORT_SYMBOL_GPL(lm3533_update); |
| 140 | |
Johan Hovold | d9055dc | 2012-05-10 14:11:28 +0200 | [diff] [blame^] | 141 | static int lm3533_set_boost_freq(struct lm3533 *lm3533, |
| 142 | enum lm3533_boost_freq freq) |
| 143 | { |
| 144 | int ret; |
| 145 | |
| 146 | ret = lm3533_update(lm3533, LM3533_REG_BOOST_PWM, |
| 147 | freq << LM3533_BOOST_FREQ_SHIFT, |
| 148 | LM3533_BOOST_FREQ_MASK); |
| 149 | if (ret) |
| 150 | dev_err(lm3533->dev, "failed to set boost frequency\n"); |
| 151 | |
| 152 | return ret; |
| 153 | } |
| 154 | |
| 155 | |
| 156 | static int lm3533_set_boost_ovp(struct lm3533 *lm3533, |
| 157 | enum lm3533_boost_ovp ovp) |
| 158 | { |
| 159 | int ret; |
| 160 | |
| 161 | ret = lm3533_update(lm3533, LM3533_REG_BOOST_PWM, |
| 162 | ovp << LM3533_BOOST_OVP_SHIFT, |
| 163 | LM3533_BOOST_OVP_MASK); |
| 164 | if (ret) |
| 165 | dev_err(lm3533->dev, "failed to set boost ovp\n"); |
| 166 | |
| 167 | return ret; |
| 168 | } |
| 169 | |
Johan Hovold | 16c5c02 | 2012-05-03 12:26:36 +0200 | [diff] [blame] | 170 | /* |
| 171 | * HVLED output config -- output hvled controlled by backlight bl |
| 172 | */ |
| 173 | static int lm3533_set_hvled_config(struct lm3533 *lm3533, u8 hvled, u8 bl) |
| 174 | { |
| 175 | u8 val; |
| 176 | u8 mask; |
| 177 | int shift; |
| 178 | int ret; |
| 179 | |
| 180 | if (hvled == 0 || hvled > LM3533_HVLED_ID_MAX) |
| 181 | return -EINVAL; |
| 182 | |
| 183 | if (bl > LM3533_BL_ID_MAX) |
| 184 | return -EINVAL; |
| 185 | |
| 186 | shift = hvled - 1; |
| 187 | mask = LM3533_BL_ID_MASK << shift; |
| 188 | val = bl << shift; |
| 189 | |
| 190 | ret = lm3533_update(lm3533, LM3533_REG_OUTPUT_CONF1, val, mask); |
| 191 | if (ret) |
| 192 | dev_err(lm3533->dev, "failed to set hvled config\n"); |
| 193 | |
| 194 | return ret; |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * LVLED output config -- output lvled controlled by LED led |
| 199 | */ |
| 200 | static int lm3533_set_lvled_config(struct lm3533 *lm3533, u8 lvled, u8 led) |
| 201 | { |
| 202 | u8 reg; |
| 203 | u8 val; |
| 204 | u8 mask; |
| 205 | int shift; |
| 206 | int ret; |
| 207 | |
| 208 | if (lvled == 0 || lvled > LM3533_LVLED_ID_MAX) |
| 209 | return -EINVAL; |
| 210 | |
| 211 | if (led > LM3533_LED_ID_MAX) |
| 212 | return -EINVAL; |
| 213 | |
| 214 | if (lvled < 4) { |
| 215 | reg = LM3533_REG_OUTPUT_CONF1; |
| 216 | shift = 2 * lvled; |
| 217 | } else { |
| 218 | reg = LM3533_REG_OUTPUT_CONF2; |
| 219 | shift = 2 * (lvled - 4); |
| 220 | } |
| 221 | |
| 222 | mask = LM3533_LED_ID_MASK << shift; |
| 223 | val = led << shift; |
| 224 | |
| 225 | ret = lm3533_update(lm3533, reg, val, mask); |
| 226 | if (ret) |
| 227 | dev_err(lm3533->dev, "failed to set lvled config\n"); |
| 228 | |
| 229 | return ret; |
| 230 | } |
| 231 | |
| 232 | static void lm3533_enable(struct lm3533 *lm3533) |
| 233 | { |
| 234 | if (gpio_is_valid(lm3533->gpio_hwen)) |
| 235 | gpio_set_value(lm3533->gpio_hwen, 1); |
| 236 | } |
| 237 | |
| 238 | static void lm3533_disable(struct lm3533 *lm3533) |
| 239 | { |
| 240 | if (gpio_is_valid(lm3533->gpio_hwen)) |
| 241 | gpio_set_value(lm3533->gpio_hwen, 0); |
| 242 | } |
| 243 | |
| 244 | enum lm3533_attribute_type { |
| 245 | LM3533_ATTR_TYPE_BACKLIGHT, |
| 246 | LM3533_ATTR_TYPE_LED, |
| 247 | }; |
| 248 | |
| 249 | struct lm3533_device_attribute { |
| 250 | struct device_attribute dev_attr; |
| 251 | enum lm3533_attribute_type type; |
| 252 | union { |
| 253 | struct { |
| 254 | u8 id; |
| 255 | } output; |
| 256 | struct { |
| 257 | u8 reg; |
| 258 | u8 shift; |
| 259 | u8 mask; |
| 260 | u8 max; |
| 261 | } generic; |
| 262 | } u; |
| 263 | }; |
| 264 | |
| 265 | #define to_lm3533_dev_attr(_attr) \ |
| 266 | container_of(_attr, struct lm3533_device_attribute, dev_attr) |
| 267 | |
| 268 | static ssize_t show_lm3533_reg(struct device *dev, |
| 269 | struct device_attribute *attr, char *buf) |
| 270 | { |
| 271 | struct lm3533 *lm3533 = dev_get_drvdata(dev); |
| 272 | struct lm3533_device_attribute *lattr = to_lm3533_dev_attr(attr); |
| 273 | u8 val; |
| 274 | int ret; |
| 275 | |
| 276 | ret = lm3533_read(lm3533, lattr->u.generic.reg, &val); |
| 277 | if (ret) |
| 278 | return ret; |
| 279 | |
| 280 | val = (val & lattr->u.generic.mask) >> lattr->u.generic.shift; |
| 281 | |
| 282 | return scnprintf(buf, PAGE_SIZE, "%u\n", val); |
| 283 | } |
| 284 | |
| 285 | static ssize_t store_lm3533_reg(struct device *dev, |
| 286 | struct device_attribute *attr, |
| 287 | const char *buf, size_t len) |
| 288 | { |
| 289 | struct lm3533 *lm3533 = dev_get_drvdata(dev); |
| 290 | struct lm3533_device_attribute *lattr = to_lm3533_dev_attr(attr); |
| 291 | u8 val; |
| 292 | int ret; |
| 293 | |
| 294 | if (kstrtou8(buf, 0, &val) || val > lattr->u.generic.max) |
| 295 | return -EINVAL; |
| 296 | |
| 297 | val = val << lattr->u.generic.shift; |
| 298 | ret = lm3533_update(lm3533, lattr->u.generic.reg, val, |
| 299 | lattr->u.generic.mask); |
| 300 | if (ret) |
| 301 | return ret; |
| 302 | |
| 303 | return len; |
| 304 | } |
| 305 | |
| 306 | #define GENERIC_ATTR(_reg, _max, _mask, _shift) \ |
| 307 | { .reg = _reg, \ |
| 308 | .max = _max, \ |
| 309 | .mask = _mask, \ |
| 310 | .shift = _shift } |
| 311 | |
| 312 | #define LM3533_GENERIC_ATTR(_name, _mode, _show, _store, _type, \ |
| 313 | _reg, _max, _mask, _shift) \ |
| 314 | struct lm3533_device_attribute lm3533_dev_attr_##_name = { \ |
| 315 | .dev_attr = __ATTR(_name, _mode, _show, _store), \ |
| 316 | .type = _type, \ |
| 317 | .u.generic = GENERIC_ATTR(_reg, _max, _mask, _shift) } |
| 318 | |
| 319 | #define LM3533_GENERIC_ATTR_RW(_name, _type, _reg, _max, _mask, _shift) \ |
| 320 | LM3533_GENERIC_ATTR(_name, S_IRUGO | S_IWUSR, \ |
| 321 | show_lm3533_reg, store_lm3533_reg, \ |
| 322 | _type, _reg, _max, _mask, _shift) |
| 323 | |
| 324 | #define LM3533_BOOST_ATTR_RW(_name, _NAME) \ |
| 325 | LM3533_GENERIC_ATTR_RW(_name, LM3533_ATTR_TYPE_BACKLIGHT, \ |
| 326 | LM3533_REG_BOOST_PWM, LM3533_##_NAME##_MAX, \ |
| 327 | LM3533_##_NAME##_MASK, LM3533_##_NAME##_SHIFT) |
| 328 | /* |
| 329 | * Boost Over Voltage Protection Select |
| 330 | * |
| 331 | * 0 - 16 V (default) |
| 332 | * 1 - 24 V |
| 333 | * 2 - 32 V |
| 334 | * 3 - 40 V |
| 335 | */ |
| 336 | static LM3533_BOOST_ATTR_RW(boost_ovp, BOOST_OVP); |
| 337 | |
| 338 | /* |
| 339 | * Boost Frequency Select |
| 340 | * |
| 341 | * 0 - 500 kHz (default) |
| 342 | * 1 - 1 MHz |
| 343 | */ |
| 344 | static LM3533_BOOST_ATTR_RW(boost_freq, BOOST_FREQ); |
| 345 | |
| 346 | static ssize_t show_output(struct device *dev, |
| 347 | struct device_attribute *attr, char *buf) |
| 348 | { |
| 349 | struct lm3533 *lm3533 = dev_get_drvdata(dev); |
| 350 | struct lm3533_device_attribute *lattr = to_lm3533_dev_attr(attr); |
| 351 | int id = lattr->u.output.id; |
| 352 | u8 reg; |
| 353 | u8 val; |
| 354 | u8 mask; |
| 355 | int shift; |
| 356 | int ret; |
| 357 | |
| 358 | if (lattr->type == LM3533_ATTR_TYPE_BACKLIGHT) { |
| 359 | reg = LM3533_REG_OUTPUT_CONF1; |
| 360 | shift = id - 1; |
| 361 | mask = LM3533_BL_ID_MASK << shift; |
| 362 | } else { |
| 363 | if (id < 4) { |
| 364 | reg = LM3533_REG_OUTPUT_CONF1; |
| 365 | shift = 2 * id; |
| 366 | } else { |
| 367 | reg = LM3533_REG_OUTPUT_CONF2; |
| 368 | shift = 2 * (id - 4); |
| 369 | } |
| 370 | mask = LM3533_LED_ID_MASK << shift; |
| 371 | } |
| 372 | |
| 373 | ret = lm3533_read(lm3533, reg, &val); |
| 374 | if (ret) |
| 375 | return ret; |
| 376 | |
| 377 | val = (val & mask) >> shift; |
| 378 | |
| 379 | return scnprintf(buf, PAGE_SIZE, "%u\n", val); |
| 380 | } |
| 381 | |
| 382 | static ssize_t store_output(struct device *dev, |
| 383 | struct device_attribute *attr, |
| 384 | const char *buf, size_t len) |
| 385 | { |
| 386 | struct lm3533 *lm3533 = dev_get_drvdata(dev); |
| 387 | struct lm3533_device_attribute *lattr = to_lm3533_dev_attr(attr); |
| 388 | int id = lattr->u.output.id; |
| 389 | u8 val; |
| 390 | int ret; |
| 391 | |
| 392 | if (kstrtou8(buf, 0, &val)) |
| 393 | return -EINVAL; |
| 394 | |
| 395 | if (lattr->type == LM3533_ATTR_TYPE_BACKLIGHT) |
| 396 | ret = lm3533_set_hvled_config(lm3533, id, val); |
| 397 | else |
| 398 | ret = lm3533_set_lvled_config(lm3533, id, val); |
| 399 | |
| 400 | if (ret) |
| 401 | return ret; |
| 402 | |
| 403 | return len; |
| 404 | } |
| 405 | |
| 406 | #define LM3533_OUTPUT_ATTR(_name, _mode, _show, _store, _type, _id) \ |
| 407 | struct lm3533_device_attribute lm3533_dev_attr_##_name = \ |
| 408 | { .dev_attr = __ATTR(_name, _mode, _show, _store), \ |
| 409 | .type = _type, \ |
| 410 | .u.output = { .id = _id }, } |
| 411 | |
| 412 | #define LM3533_OUTPUT_ATTR_RW(_name, _type, _id) \ |
| 413 | LM3533_OUTPUT_ATTR(output_##_name, S_IRUGO | S_IWUSR, \ |
| 414 | show_output, store_output, _type, _id) |
| 415 | |
| 416 | #define LM3533_OUTPUT_HVLED_ATTR_RW(_nr) \ |
| 417 | LM3533_OUTPUT_ATTR_RW(hvled##_nr, LM3533_ATTR_TYPE_BACKLIGHT, _nr) |
| 418 | #define LM3533_OUTPUT_LVLED_ATTR_RW(_nr) \ |
| 419 | LM3533_OUTPUT_ATTR_RW(lvled##_nr, LM3533_ATTR_TYPE_LED, _nr) |
| 420 | /* |
| 421 | * Output config: |
| 422 | * |
| 423 | * output_hvled<nr> 0-1 |
| 424 | * output_lvled<nr> 0-3 |
| 425 | */ |
| 426 | static LM3533_OUTPUT_HVLED_ATTR_RW(1); |
| 427 | static LM3533_OUTPUT_HVLED_ATTR_RW(2); |
| 428 | static LM3533_OUTPUT_LVLED_ATTR_RW(1); |
| 429 | static LM3533_OUTPUT_LVLED_ATTR_RW(2); |
| 430 | static LM3533_OUTPUT_LVLED_ATTR_RW(3); |
| 431 | static LM3533_OUTPUT_LVLED_ATTR_RW(4); |
| 432 | static LM3533_OUTPUT_LVLED_ATTR_RW(5); |
| 433 | |
| 434 | static struct attribute *lm3533_attributes[] = { |
| 435 | &lm3533_dev_attr_boost_freq.dev_attr.attr, |
| 436 | &lm3533_dev_attr_boost_ovp.dev_attr.attr, |
| 437 | &lm3533_dev_attr_output_hvled1.dev_attr.attr, |
| 438 | &lm3533_dev_attr_output_hvled2.dev_attr.attr, |
| 439 | &lm3533_dev_attr_output_lvled1.dev_attr.attr, |
| 440 | &lm3533_dev_attr_output_lvled2.dev_attr.attr, |
| 441 | &lm3533_dev_attr_output_lvled3.dev_attr.attr, |
| 442 | &lm3533_dev_attr_output_lvled4.dev_attr.attr, |
| 443 | &lm3533_dev_attr_output_lvled5.dev_attr.attr, |
| 444 | NULL, |
| 445 | }; |
| 446 | |
| 447 | #define to_dev_attr(_attr) \ |
| 448 | container_of(_attr, struct device_attribute, attr) |
| 449 | |
| 450 | static mode_t lm3533_attr_is_visible(struct kobject *kobj, |
| 451 | struct attribute *attr, int n) |
| 452 | { |
| 453 | struct device *dev = container_of(kobj, struct device, kobj); |
| 454 | struct lm3533 *lm3533 = dev_get_drvdata(dev); |
| 455 | struct device_attribute *dattr = to_dev_attr(attr); |
| 456 | struct lm3533_device_attribute *lattr = to_lm3533_dev_attr(dattr); |
| 457 | enum lm3533_attribute_type type = lattr->type; |
| 458 | mode_t mode = attr->mode; |
| 459 | |
| 460 | if (!lm3533->have_backlights && type == LM3533_ATTR_TYPE_BACKLIGHT) |
| 461 | mode = 0; |
| 462 | else if (!lm3533->have_leds && type == LM3533_ATTR_TYPE_LED) |
| 463 | mode = 0; |
| 464 | |
| 465 | return mode; |
| 466 | }; |
| 467 | |
| 468 | static struct attribute_group lm3533_attribute_group = { |
| 469 | .is_visible = lm3533_attr_is_visible, |
| 470 | .attrs = lm3533_attributes |
| 471 | }; |
| 472 | |
| 473 | static int __devinit lm3533_device_als_init(struct lm3533 *lm3533) |
| 474 | { |
| 475 | struct lm3533_platform_data *pdata = lm3533->dev->platform_data; |
| 476 | int ret; |
| 477 | |
| 478 | if (!pdata->als) |
| 479 | return 0; |
| 480 | |
| 481 | lm3533_als_devs[0].platform_data = pdata->als; |
| 482 | lm3533_als_devs[0].pdata_size = sizeof(*pdata->als); |
| 483 | |
| 484 | ret = mfd_add_devices(lm3533->dev, 0, lm3533_als_devs, 1, NULL, 0); |
| 485 | if (ret) { |
| 486 | dev_err(lm3533->dev, "failed to add ALS device\n"); |
| 487 | return ret; |
| 488 | } |
| 489 | |
| 490 | lm3533->have_als = 1; |
| 491 | |
| 492 | return 0; |
| 493 | } |
| 494 | |
| 495 | static int __devinit lm3533_device_bl_init(struct lm3533 *lm3533) |
| 496 | { |
| 497 | struct lm3533_platform_data *pdata = lm3533->dev->platform_data; |
| 498 | int i; |
| 499 | int ret; |
| 500 | |
| 501 | if (!pdata->backlights || pdata->num_backlights == 0) |
| 502 | return 0; |
| 503 | |
| 504 | if (pdata->num_backlights > ARRAY_SIZE(lm3533_bl_devs)) |
| 505 | pdata->num_backlights = ARRAY_SIZE(lm3533_bl_devs); |
| 506 | |
| 507 | for (i = 0; i < pdata->num_backlights; ++i) { |
| 508 | lm3533_bl_devs[i].platform_data = &pdata->backlights[i]; |
| 509 | lm3533_bl_devs[i].pdata_size = sizeof(pdata->backlights[i]); |
| 510 | } |
| 511 | |
| 512 | ret = mfd_add_devices(lm3533->dev, 0, lm3533_bl_devs, |
| 513 | pdata->num_backlights, NULL, 0); |
| 514 | if (ret) { |
| 515 | dev_err(lm3533->dev, "failed to add backlight devices\n"); |
| 516 | return ret; |
| 517 | } |
| 518 | |
| 519 | lm3533->have_backlights = 1; |
| 520 | |
| 521 | return 0; |
| 522 | } |
| 523 | |
| 524 | static int __devinit lm3533_device_led_init(struct lm3533 *lm3533) |
| 525 | { |
| 526 | struct lm3533_platform_data *pdata = lm3533->dev->platform_data; |
| 527 | int i; |
| 528 | int ret; |
| 529 | |
| 530 | if (!pdata->leds || pdata->num_leds == 0) |
| 531 | return 0; |
| 532 | |
| 533 | if (pdata->num_leds > ARRAY_SIZE(lm3533_led_devs)) |
| 534 | pdata->num_leds = ARRAY_SIZE(lm3533_led_devs); |
| 535 | |
| 536 | for (i = 0; i < pdata->num_leds; ++i) { |
| 537 | lm3533_led_devs[i].platform_data = &pdata->leds[i]; |
| 538 | lm3533_led_devs[i].pdata_size = sizeof(pdata->leds[i]); |
| 539 | } |
| 540 | |
| 541 | ret = mfd_add_devices(lm3533->dev, 0, lm3533_led_devs, |
| 542 | pdata->num_leds, NULL, 0); |
| 543 | if (ret) { |
| 544 | dev_err(lm3533->dev, "failed to add LED devices\n"); |
| 545 | return ret; |
| 546 | } |
| 547 | |
| 548 | lm3533->have_leds = 1; |
| 549 | |
| 550 | return 0; |
| 551 | } |
| 552 | |
Johan Hovold | d9055dc | 2012-05-10 14:11:28 +0200 | [diff] [blame^] | 553 | static int __devinit lm3533_device_setup(struct lm3533 *lm3533, |
| 554 | struct lm3533_platform_data *pdata) |
| 555 | { |
| 556 | int ret; |
| 557 | |
| 558 | ret = lm3533_set_boost_freq(lm3533, pdata->boost_freq); |
| 559 | if (ret) |
| 560 | return ret; |
| 561 | |
| 562 | ret = lm3533_set_boost_ovp(lm3533, pdata->boost_ovp); |
| 563 | if (ret) |
| 564 | return ret; |
| 565 | |
| 566 | return 0; |
| 567 | } |
| 568 | |
Johan Hovold | 16c5c02 | 2012-05-03 12:26:36 +0200 | [diff] [blame] | 569 | static int __devinit lm3533_device_init(struct lm3533 *lm3533) |
| 570 | { |
| 571 | struct lm3533_platform_data *pdata = lm3533->dev->platform_data; |
| 572 | int ret; |
| 573 | |
| 574 | dev_dbg(lm3533->dev, "%s\n", __func__); |
| 575 | |
| 576 | if (!pdata) { |
| 577 | dev_err(lm3533->dev, "no platform data\n"); |
| 578 | return -EINVAL; |
| 579 | } |
| 580 | |
| 581 | lm3533->gpio_hwen = pdata->gpio_hwen; |
| 582 | |
| 583 | dev_set_drvdata(lm3533->dev, lm3533); |
| 584 | |
| 585 | if (gpio_is_valid(lm3533->gpio_hwen)) { |
| 586 | ret = gpio_request_one(lm3533->gpio_hwen, GPIOF_OUT_INIT_LOW, |
| 587 | "lm3533-hwen"); |
| 588 | if (ret < 0) { |
| 589 | dev_err(lm3533->dev, |
| 590 | "failed to request HWEN GPIO %d\n", |
| 591 | lm3533->gpio_hwen); |
| 592 | return ret; |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | lm3533_enable(lm3533); |
| 597 | |
Johan Hovold | d9055dc | 2012-05-10 14:11:28 +0200 | [diff] [blame^] | 598 | ret = lm3533_device_setup(lm3533, pdata); |
| 599 | if (ret) |
| 600 | goto err_disable; |
| 601 | |
Johan Hovold | 16c5c02 | 2012-05-03 12:26:36 +0200 | [diff] [blame] | 602 | lm3533_device_als_init(lm3533); |
| 603 | lm3533_device_bl_init(lm3533); |
| 604 | lm3533_device_led_init(lm3533); |
| 605 | |
| 606 | ret = sysfs_create_group(&lm3533->dev->kobj, &lm3533_attribute_group); |
| 607 | if (ret < 0) { |
| 608 | dev_err(lm3533->dev, "failed to create sysfs attributes\n"); |
| 609 | goto err_unregister; |
| 610 | } |
| 611 | |
| 612 | return 0; |
| 613 | |
| 614 | err_unregister: |
| 615 | mfd_remove_devices(lm3533->dev); |
Johan Hovold | d9055dc | 2012-05-10 14:11:28 +0200 | [diff] [blame^] | 616 | err_disable: |
Johan Hovold | 16c5c02 | 2012-05-03 12:26:36 +0200 | [diff] [blame] | 617 | lm3533_disable(lm3533); |
| 618 | if (gpio_is_valid(lm3533->gpio_hwen)) |
| 619 | gpio_free(lm3533->gpio_hwen); |
| 620 | |
| 621 | return ret; |
| 622 | } |
| 623 | |
| 624 | static void __devexit lm3533_device_exit(struct lm3533 *lm3533) |
| 625 | { |
| 626 | dev_dbg(lm3533->dev, "%s\n", __func__); |
| 627 | |
| 628 | sysfs_remove_group(&lm3533->dev->kobj, &lm3533_attribute_group); |
| 629 | |
| 630 | mfd_remove_devices(lm3533->dev); |
| 631 | lm3533_disable(lm3533); |
| 632 | if (gpio_is_valid(lm3533->gpio_hwen)) |
| 633 | gpio_free(lm3533->gpio_hwen); |
| 634 | } |
| 635 | |
| 636 | static bool lm3533_readable_register(struct device *dev, unsigned int reg) |
| 637 | { |
| 638 | switch (reg) { |
| 639 | case 0x10 ... 0x2c: |
| 640 | case 0x30 ... 0x38: |
| 641 | case 0x40 ... 0x45: |
| 642 | case 0x50 ... 0x57: |
| 643 | case 0x60 ... 0x6e: |
| 644 | case 0x70 ... 0x75: |
| 645 | case 0x80 ... 0x85: |
| 646 | case 0x90 ... 0x95: |
| 647 | case 0xa0 ... 0xa5: |
| 648 | case 0xb0 ... 0xb2: |
| 649 | return true; |
| 650 | default: |
| 651 | return false; |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | static bool lm3533_volatile_register(struct device *dev, unsigned int reg) |
| 656 | { |
| 657 | switch (reg) { |
| 658 | case 0x34: /* zone */ |
| 659 | case 0x37 ... 0x38: /* adc */ |
| 660 | case 0xb0 ... 0xb1: /* fault */ |
| 661 | return true; |
| 662 | default: |
| 663 | return false; |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | static bool lm3533_precious_register(struct device *dev, unsigned int reg) |
| 668 | { |
| 669 | switch (reg) { |
| 670 | case 0x34: /* zone */ |
| 671 | return true; |
| 672 | default: |
| 673 | return false; |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | static struct regmap_config regmap_config = { |
| 678 | .reg_bits = 8, |
| 679 | .val_bits = 8, |
| 680 | .max_register = LM3533_REG_MAX, |
| 681 | .readable_reg = lm3533_readable_register, |
| 682 | .volatile_reg = lm3533_volatile_register, |
| 683 | .precious_reg = lm3533_precious_register, |
| 684 | }; |
| 685 | |
| 686 | static int __devinit lm3533_i2c_probe(struct i2c_client *i2c, |
| 687 | const struct i2c_device_id *id) |
| 688 | { |
| 689 | struct lm3533 *lm3533; |
| 690 | int ret; |
| 691 | |
| 692 | dev_dbg(&i2c->dev, "%s\n", __func__); |
| 693 | |
| 694 | lm3533 = kzalloc(sizeof(*lm3533), GFP_KERNEL); |
| 695 | if (!lm3533) |
| 696 | return -ENOMEM; |
| 697 | |
| 698 | i2c_set_clientdata(i2c, lm3533); |
| 699 | |
| 700 | lm3533->regmap = regmap_init_i2c(i2c, ®map_config); |
| 701 | if (IS_ERR(lm3533->regmap)) { |
| 702 | ret = PTR_ERR(lm3533->regmap); |
| 703 | goto err_regmap; |
| 704 | } |
| 705 | |
| 706 | lm3533->dev = &i2c->dev; |
| 707 | lm3533->irq = i2c->irq; |
| 708 | |
| 709 | ret = lm3533_device_init(lm3533); |
| 710 | if (ret) |
| 711 | goto err_dev; |
| 712 | |
| 713 | return 0; |
| 714 | |
| 715 | err_dev: |
| 716 | regmap_exit(lm3533->regmap); |
| 717 | err_regmap: |
| 718 | kfree(lm3533); |
| 719 | |
| 720 | return ret; |
| 721 | } |
| 722 | |
| 723 | static int __devexit lm3533_i2c_remove(struct i2c_client *i2c) |
| 724 | { |
| 725 | struct lm3533 *lm3533 = i2c_get_clientdata(i2c); |
| 726 | |
| 727 | dev_dbg(&i2c->dev, "%s\n", __func__); |
| 728 | |
| 729 | lm3533_device_exit(lm3533); |
| 730 | regmap_exit(lm3533->regmap); |
| 731 | |
| 732 | kfree(lm3533); |
| 733 | |
| 734 | return 0; |
| 735 | } |
| 736 | |
| 737 | static const struct i2c_device_id lm3533_i2c_ids[] = { |
| 738 | { "lm3533", 0 }, |
| 739 | { }, |
| 740 | }; |
| 741 | MODULE_DEVICE_TABLE(i2c, lm3533_i2c_ids); |
| 742 | |
| 743 | static struct i2c_driver lm3533_i2c_driver = { |
| 744 | .driver = { |
| 745 | .name = "lm3533", |
| 746 | .owner = THIS_MODULE, |
| 747 | }, |
| 748 | .id_table = lm3533_i2c_ids, |
| 749 | .probe = lm3533_i2c_probe, |
| 750 | .remove = __devexit_p(lm3533_i2c_remove), |
| 751 | }; |
| 752 | |
| 753 | static int __init lm3533_i2c_init(void) |
| 754 | { |
| 755 | return i2c_add_driver(&lm3533_i2c_driver); |
| 756 | } |
| 757 | subsys_initcall(lm3533_i2c_init); |
| 758 | |
| 759 | static void __exit lm3533_i2c_exit(void) |
| 760 | { |
| 761 | i2c_del_driver(&lm3533_i2c_driver); |
| 762 | } |
| 763 | module_exit(lm3533_i2c_exit); |
| 764 | |
| 765 | MODULE_AUTHOR("Johan Hovold <jhovold@gmail.com>"); |
| 766 | MODULE_DESCRIPTION("LM3533 Core"); |
| 767 | MODULE_LICENSE("GPL"); |