1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * lm3533-core.c -- LM3533 Core 4 * 5 * Copyright (C) 2011-2012 Texas Instruments 6 * 7 * Author: Johan Hovold <jhovold@gmail.com> 8 */ 9 10 #include <linux/bitfield.h> 11 #include <linux/module.h> 12 #include <linux/init.h> 13 #include <linux/kernel.h> 14 #include <linux/err.h> 15 #include <linux/gpio/consumer.h> 16 #include <linux/i2c.h> 17 #include <linux/mfd/core.h> 18 #include <linux/property.h> 19 #include <linux/regmap.h> 20 #include <linux/regulator/consumer.h> 21 #include <linux/seq_file.h> 22 #include <linux/slab.h> 23 #include <linux/uaccess.h> 24 #include <linux/units.h> 25 26 #include <linux/mfd/lm3533.h> 27 28 29 #define LM3533_BOOST_OVP_MASK 0x06 30 #define LM3533_BOOST_OVP_MIN (16 * MICRO) 31 #define LM3533_BOOST_OVP_MAX (40 * MICRO) 32 33 #define LM3533_BOOST_FREQ_MASK 0x01 34 #define LM3533_BOOST_FREQ_MIN (500 * HZ_PER_KHZ) 35 #define LM3533_BOOST_FREQ_MAX (1000 * HZ_PER_KHZ) 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 #define LM3533_CELLS_MAX 7 45 46 #define LM3533_REG_OUTPUT_CONF1 0x10 47 #define LM3533_REG_OUTPUT_CONF2 0x11 48 #define LM3533_REG_BOOST_PWM 0x2c 49 50 #define LM3533_REG_MAX 0xb2 51 52 /* 53 * HVLED output config -- output hvled controlled by backlight bl 54 */ 55 static int lm3533_set_hvled_config(struct lm3533 *lm3533, u8 hvled, u8 bl) 56 { 57 u8 val; 58 u8 mask; 59 int shift; 60 int ret; 61 62 if (hvled == 0 || hvled > LM3533_HVLED_ID_MAX) 63 return -EINVAL; 64 65 if (bl > LM3533_BL_ID_MAX) 66 return -EINVAL; 67 68 shift = hvled - 1; 69 mask = LM3533_BL_ID_MASK << shift; 70 val = bl << shift; 71 72 ret = regmap_update_bits(lm3533->regmap, LM3533_REG_OUTPUT_CONF1, 73 mask, val); 74 if (ret) 75 dev_err(lm3533->dev, "failed to set hvled config\n"); 76 77 return ret; 78 } 79 80 /* 81 * LVLED output config -- output lvled controlled by LED led 82 */ 83 static int lm3533_set_lvled_config(struct lm3533 *lm3533, u8 lvled, u8 led) 84 { 85 u8 reg; 86 u8 val; 87 u8 mask; 88 int shift; 89 int ret; 90 91 if (lvled == 0 || lvled > LM3533_LVLED_ID_MAX) 92 return -EINVAL; 93 94 if (led > LM3533_LED_ID_MAX) 95 return -EINVAL; 96 97 if (lvled < 4) { 98 reg = LM3533_REG_OUTPUT_CONF1; 99 shift = 2 * lvled; 100 } else { 101 reg = LM3533_REG_OUTPUT_CONF2; 102 shift = 2 * (lvled - 4); 103 } 104 105 mask = LM3533_LED_ID_MASK << shift; 106 val = led << shift; 107 108 ret = regmap_update_bits(lm3533->regmap, reg, mask, val); 109 if (ret) 110 dev_err(lm3533->dev, "failed to set lvled config\n"); 111 112 return ret; 113 } 114 115 static int lm3533_enable(struct lm3533 *lm3533) 116 { 117 int ret; 118 119 ret = regulator_enable(lm3533->vin_supply); 120 if (ret) { 121 dev_err(lm3533->dev, "failed to enable vin power supply\n"); 122 return ret; 123 } 124 125 gpiod_set_value(lm3533->hwen, 1); 126 127 return 0; 128 } 129 130 static void lm3533_disable(struct lm3533 *lm3533) 131 { 132 gpiod_set_value(lm3533->hwen, 0); 133 regulator_disable(lm3533->vin_supply); 134 } 135 136 enum lm3533_attribute_type { 137 LM3533_ATTR_TYPE_BACKLIGHT, 138 LM3533_ATTR_TYPE_LED, 139 }; 140 141 struct lm3533_device_attribute { 142 struct device_attribute dev_attr; 143 enum lm3533_attribute_type type; 144 union { 145 struct { 146 u8 id; 147 } output; 148 } u; 149 }; 150 151 #define to_lm3533_dev_attr(_attr) \ 152 container_of(_attr, struct lm3533_device_attribute, dev_attr) 153 154 static ssize_t show_output(struct device *dev, 155 struct device_attribute *attr, char *buf) 156 { 157 struct lm3533 *lm3533 = dev_get_drvdata(dev); 158 struct lm3533_device_attribute *lattr = to_lm3533_dev_attr(attr); 159 int id = lattr->u.output.id; 160 u8 reg; 161 u32 val; 162 u8 mask; 163 int shift; 164 int ret; 165 166 if (lattr->type == LM3533_ATTR_TYPE_BACKLIGHT) { 167 reg = LM3533_REG_OUTPUT_CONF1; 168 shift = id - 1; 169 mask = LM3533_BL_ID_MASK << shift; 170 } else { 171 if (id < 4) { 172 reg = LM3533_REG_OUTPUT_CONF1; 173 shift = 2 * id; 174 } else { 175 reg = LM3533_REG_OUTPUT_CONF2; 176 shift = 2 * (id - 4); 177 } 178 mask = LM3533_LED_ID_MASK << shift; 179 } 180 181 ret = regmap_read(lm3533->regmap, reg, &val); 182 if (ret) 183 return ret; 184 185 val = (val & mask) >> shift; 186 187 return sysfs_emit(buf, "%u\n", val); 188 } 189 190 static ssize_t store_output(struct device *dev, 191 struct device_attribute *attr, 192 const char *buf, size_t len) 193 { 194 struct lm3533 *lm3533 = dev_get_drvdata(dev); 195 struct lm3533_device_attribute *lattr = to_lm3533_dev_attr(attr); 196 int id = lattr->u.output.id; 197 u8 val; 198 int ret; 199 200 if (kstrtou8(buf, 0, &val)) 201 return -EINVAL; 202 203 if (lattr->type == LM3533_ATTR_TYPE_BACKLIGHT) 204 ret = lm3533_set_hvled_config(lm3533, id, val); 205 else 206 ret = lm3533_set_lvled_config(lm3533, id, val); 207 208 if (ret) 209 return ret; 210 211 return len; 212 } 213 214 #define LM3533_OUTPUT_ATTR(_name, _mode, _show, _store, _type, _id) \ 215 struct lm3533_device_attribute lm3533_dev_attr_##_name = \ 216 { .dev_attr = __ATTR(_name, _mode, _show, _store), \ 217 .type = _type, \ 218 .u.output = { .id = _id }, } 219 220 #define LM3533_OUTPUT_ATTR_RW(_name, _type, _id) \ 221 LM3533_OUTPUT_ATTR(output_##_name, S_IRUGO | S_IWUSR, \ 222 show_output, store_output, _type, _id) 223 224 #define LM3533_OUTPUT_HVLED_ATTR_RW(_nr) \ 225 LM3533_OUTPUT_ATTR_RW(hvled##_nr, LM3533_ATTR_TYPE_BACKLIGHT, _nr) 226 #define LM3533_OUTPUT_LVLED_ATTR_RW(_nr) \ 227 LM3533_OUTPUT_ATTR_RW(lvled##_nr, LM3533_ATTR_TYPE_LED, _nr) 228 /* 229 * Output config: 230 * 231 * output_hvled<nr> 0-1 232 * output_lvled<nr> 0-3 233 */ 234 static LM3533_OUTPUT_HVLED_ATTR_RW(1); 235 static LM3533_OUTPUT_HVLED_ATTR_RW(2); 236 static LM3533_OUTPUT_LVLED_ATTR_RW(1); 237 static LM3533_OUTPUT_LVLED_ATTR_RW(2); 238 static LM3533_OUTPUT_LVLED_ATTR_RW(3); 239 static LM3533_OUTPUT_LVLED_ATTR_RW(4); 240 static LM3533_OUTPUT_LVLED_ATTR_RW(5); 241 242 static struct attribute *lm3533_attributes[] = { 243 &lm3533_dev_attr_output_hvled1.dev_attr.attr, 244 &lm3533_dev_attr_output_hvled2.dev_attr.attr, 245 &lm3533_dev_attr_output_lvled1.dev_attr.attr, 246 &lm3533_dev_attr_output_lvled2.dev_attr.attr, 247 &lm3533_dev_attr_output_lvled3.dev_attr.attr, 248 &lm3533_dev_attr_output_lvled4.dev_attr.attr, 249 &lm3533_dev_attr_output_lvled5.dev_attr.attr, 250 NULL, 251 }; 252 253 #define to_dev_attr(_attr) \ 254 container_of(_attr, struct device_attribute, attr) 255 256 static umode_t lm3533_attr_is_visible(struct kobject *kobj, 257 struct attribute *attr, int n) 258 { 259 struct device *dev = kobj_to_dev(kobj); 260 struct lm3533 *lm3533 = dev_get_drvdata(dev); 261 struct device_attribute *dattr = to_dev_attr(attr); 262 struct lm3533_device_attribute *lattr = to_lm3533_dev_attr(dattr); 263 enum lm3533_attribute_type type = lattr->type; 264 umode_t mode = attr->mode; 265 266 if (!lm3533->have_backlights && type == LM3533_ATTR_TYPE_BACKLIGHT) 267 mode = 0; 268 else if (!lm3533->have_leds && type == LM3533_ATTR_TYPE_LED) 269 mode = 0; 270 271 return mode; 272 }; 273 274 static struct attribute_group lm3533_attribute_group = { 275 .is_visible = lm3533_attr_is_visible, 276 .attrs = lm3533_attributes 277 }; 278 279 static const struct attribute_group *lm3533_attribute_groups[] = { 280 &lm3533_attribute_group, 281 NULL, 282 }; 283 284 static int lm3533_device_init(struct lm3533 *lm3533) 285 { 286 struct device *dev = lm3533->dev; 287 struct mfd_cell *lm3533_devices; 288 u32 reg, nchilds; 289 u32 count = 0; 290 int ret; 291 292 nchilds = device_get_child_node_count(dev); 293 if (!nchilds || nchilds > LM3533_CELLS_MAX) 294 return dev_err_probe(dev, -ENODEV, 295 "num of child nodes is not supported\n"); 296 297 lm3533_devices = devm_kzalloc_objs(dev, *lm3533_devices, nchilds); 298 if (!lm3533_devices) 299 return -ENOMEM; 300 301 device_for_each_child_node_scoped(dev, child) { 302 if (count >= nchilds) 303 break; 304 305 if (fwnode_device_is_compatible(child, "ti,lm3533-als")) { 306 lm3533_devices[count].name = "lm3533-als"; 307 lm3533_devices[count].of_compatible = "ti,lm3533-als"; 308 lm3533_devices[count].id = PLATFORM_DEVID_NONE; 309 310 lm3533->have_als = true; 311 count++; 312 } else if (fwnode_device_is_compatible(child, "ti,lm3533-backlight")) { 313 ret = fwnode_property_read_u32(child, "reg", ®); 314 if (ret || reg >= LM3533_HVLED_ID_MAX) { 315 dev_err(dev, "invalid backlight node %pfw\n", child); 316 continue; 317 } 318 319 lm3533_devices[count].name = "lm3533-backlight"; 320 lm3533_devices[count].of_compatible = "ti,lm3533-backlight"; 321 lm3533_devices[count].id = reg; 322 lm3533_devices[count].of_reg = reg; 323 lm3533_devices[count].use_of_reg = true; 324 325 lm3533->have_backlights = true; 326 count++; 327 } else if (fwnode_device_is_compatible(child, "ti,lm3533-leds")) { 328 ret = fwnode_property_read_u32(child, "reg", ®); 329 if (ret || reg < LM3533_HVLED_ID_MAX || 330 reg > LM3533_LVLED_ID_MAX) { 331 dev_err(dev, "invalid LED node %pfw\n", child); 332 continue; 333 } 334 335 lm3533_devices[count].name = "lm3533-leds"; 336 lm3533_devices[count].of_compatible = "ti,lm3533-leds"; 337 lm3533_devices[count].id = reg - LM3533_HVLED_ID_MAX; 338 lm3533_devices[count].of_reg = reg; 339 lm3533_devices[count].use_of_reg = true; 340 341 lm3533->have_leds = true; 342 count++; 343 } 344 } 345 346 ret = lm3533_enable(lm3533); 347 if (ret) 348 return ret; 349 350 ret = regmap_update_bits(lm3533->regmap, LM3533_REG_BOOST_PWM, 351 LM3533_BOOST_FREQ_MASK, 352 FIELD_PREP(LM3533_BOOST_FREQ_MASK, lm3533->boost_freq)); 353 if (ret) { 354 dev_err(dev, "failed to set boost frequency\n"); 355 goto err_disable; 356 } 357 358 ret = regmap_update_bits(lm3533->regmap, LM3533_REG_BOOST_PWM, 359 LM3533_BOOST_OVP_MASK, 360 FIELD_PREP(LM3533_BOOST_OVP_MASK, lm3533->boost_ovp)); 361 if (ret) { 362 dev_err(dev, "failed to set boost ovp\n"); 363 goto err_disable; 364 } 365 366 ret = mfd_add_devices(dev, 0, lm3533_devices, count, NULL, 0, NULL); 367 if (ret) { 368 dev_err(dev, "failed to add MFD devices: %d\n", ret); 369 goto err_disable; 370 } 371 372 return 0; 373 374 err_disable: 375 lm3533_disable(lm3533); 376 377 return ret; 378 } 379 380 static void lm3533_device_exit(struct lm3533 *lm3533) 381 { 382 dev_dbg(lm3533->dev, "%s\n", __func__); 383 384 mfd_remove_devices(lm3533->dev); 385 lm3533_disable(lm3533); 386 } 387 388 static bool lm3533_readable_register(struct device *dev, unsigned int reg) 389 { 390 switch (reg) { 391 case 0x10 ... 0x2c: 392 case 0x30 ... 0x38: 393 case 0x40 ... 0x45: 394 case 0x50 ... 0x57: 395 case 0x60 ... 0x6e: 396 case 0x70 ... 0x75: 397 case 0x80 ... 0x85: 398 case 0x90 ... 0x95: 399 case 0xa0 ... 0xa5: 400 case 0xb0 ... 0xb2: 401 return true; 402 default: 403 return false; 404 } 405 } 406 407 static bool lm3533_volatile_register(struct device *dev, unsigned int reg) 408 { 409 switch (reg) { 410 case 0x34 ... 0x36: /* zone */ 411 case 0x37 ... 0x38: /* adc */ 412 case 0xb0 ... 0xb1: /* fault */ 413 return true; 414 default: 415 return false; 416 } 417 } 418 419 static bool lm3533_precious_register(struct device *dev, unsigned int reg) 420 { 421 switch (reg) { 422 case 0x34: /* zone */ 423 return true; 424 default: 425 return false; 426 } 427 } 428 429 static const struct regmap_config regmap_config = { 430 .reg_bits = 8, 431 .val_bits = 8, 432 .max_register = LM3533_REG_MAX, 433 .readable_reg = lm3533_readable_register, 434 .volatile_reg = lm3533_volatile_register, 435 .precious_reg = lm3533_precious_register, 436 }; 437 438 static int lm3533_i2c_probe(struct i2c_client *i2c) 439 { 440 struct lm3533 *lm3533; 441 442 dev_dbg(&i2c->dev, "%s\n", __func__); 443 444 lm3533 = devm_kzalloc_obj(&i2c->dev, *lm3533); 445 if (!lm3533) 446 return -ENOMEM; 447 448 i2c_set_clientdata(i2c, lm3533); 449 450 lm3533->regmap = devm_regmap_init_i2c(i2c, ®map_config); 451 if (IS_ERR(lm3533->regmap)) 452 return PTR_ERR(lm3533->regmap); 453 454 lm3533->dev = &i2c->dev; 455 456 lm3533->hwen = devm_gpiod_get_optional(lm3533->dev, "enable", 457 GPIOD_OUT_LOW); 458 if (IS_ERR(lm3533->hwen)) 459 return dev_err_probe(lm3533->dev, PTR_ERR(lm3533->hwen), 460 "failed to get HWEN GPIO\n"); 461 462 lm3533->vin_supply = devm_regulator_get(lm3533->dev, "vin"); 463 if (IS_ERR(lm3533->vin_supply)) 464 return dev_err_probe(lm3533->dev, PTR_ERR(lm3533->vin_supply), 465 "failed to get vin-supply\n"); 466 467 device_property_read_u32(lm3533->dev, "ti,boost-ovp-microvolt", 468 &lm3533->boost_ovp); 469 470 lm3533->boost_ovp = clamp(lm3533->boost_ovp, LM3533_BOOST_OVP_MIN, 471 LM3533_BOOST_OVP_MAX); 472 lm3533->boost_ovp = lm3533->boost_ovp / (8 * MICRO) - 2; 473 474 device_property_read_u32(lm3533->dev, "ti,boost-freq-hz", 475 &lm3533->boost_freq); 476 477 lm3533->boost_freq = clamp(lm3533->boost_freq, LM3533_BOOST_FREQ_MIN, 478 LM3533_BOOST_FREQ_MAX); 479 lm3533->boost_freq = lm3533->boost_freq / (500 * KILO) - 1; 480 481 /* LM3533 and child devices do not use DMA */ 482 i2c->dev.coherent_dma_mask = 0; 483 i2c->dev.dma_mask = &i2c->dev.coherent_dma_mask; 484 485 return lm3533_device_init(lm3533); 486 } 487 488 static void lm3533_i2c_remove(struct i2c_client *i2c) 489 { 490 struct lm3533 *lm3533 = i2c_get_clientdata(i2c); 491 492 dev_dbg(&i2c->dev, "%s\n", __func__); 493 494 lm3533_device_exit(lm3533); 495 } 496 497 static const struct of_device_id lm3533_match_table[] = { 498 { .compatible = "ti,lm3533" }, 499 { } 500 }; 501 MODULE_DEVICE_TABLE(of, lm3533_match_table); 502 503 static const struct i2c_device_id lm3533_i2c_ids[] = { 504 { "lm3533" }, 505 { } 506 }; 507 MODULE_DEVICE_TABLE(i2c, lm3533_i2c_ids); 508 509 static struct i2c_driver lm3533_i2c_driver = { 510 .driver = { 511 .name = "lm3533", 512 .dev_groups = lm3533_attribute_groups, 513 .of_match_table = lm3533_match_table, 514 }, 515 .id_table = lm3533_i2c_ids, 516 .probe = lm3533_i2c_probe, 517 .remove = lm3533_i2c_remove, 518 }; 519 520 static int __init lm3533_i2c_init(void) 521 { 522 return i2c_add_driver(&lm3533_i2c_driver); 523 } 524 subsys_initcall(lm3533_i2c_init); 525 526 static void __exit lm3533_i2c_exit(void) 527 { 528 i2c_del_driver(&lm3533_i2c_driver); 529 } 530 module_exit(lm3533_i2c_exit); 531 532 MODULE_AUTHOR("Johan Hovold <jhovold@gmail.com>"); 533 MODULE_DESCRIPTION("LM3533 Core"); 534 MODULE_LICENSE("GPL"); 535