root/drivers/leds/leds-mc13783.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. mc13xxx_max_brightness
  2. mc13xxx_led_set
  3. mc13xxx_led_probe_dt
  4. mc13xxx_led_probe_dt
  5. mc13xxx_led_probe
  6. mc13xxx_led_remove

   1 // SPDX-License-Identifier: GPL-2.0-only
   2 /*
   3  * LEDs driver for Freescale MC13783/MC13892/MC34708
   4  *
   5  * Copyright (C) 2010 Philippe Rétornaz
   6  *
   7  * Based on leds-da903x:
   8  * Copyright (C) 2008 Compulab, Ltd.
   9  *      Mike Rapoport <mike@compulab.co.il>
  10  *
  11  * Copyright (C) 2006-2008 Marvell International Ltd.
  12  *      Eric Miao <eric.miao@marvell.com>
  13  */
  14 
  15 #include <linux/module.h>
  16 #include <linux/kernel.h>
  17 #include <linux/platform_device.h>
  18 #include <linux/leds.h>
  19 #include <linux/of.h>
  20 #include <linux/mfd/mc13xxx.h>
  21 
  22 struct mc13xxx_led_devtype {
  23         int     led_min;
  24         int     led_max;
  25         int     num_regs;
  26         u32     ledctrl_base;
  27 };
  28 
  29 struct mc13xxx_led {
  30         struct led_classdev     cdev;
  31         int                     id;
  32         struct mc13xxx_leds     *leds;
  33 };
  34 
  35 struct mc13xxx_leds {
  36         struct mc13xxx                  *master;
  37         struct mc13xxx_led_devtype      *devtype;
  38         int                             num_leds;
  39         struct mc13xxx_led              *led;
  40 };
  41 
  42 static unsigned int mc13xxx_max_brightness(int id)
  43 {
  44         if (id >= MC13783_LED_MD && id <= MC13783_LED_KP)
  45                 return 0x0f;
  46         else if (id >= MC13783_LED_R1 && id <= MC13783_LED_B3)
  47                 return 0x1f;
  48 
  49         return 0x3f;
  50 }
  51 
  52 static int mc13xxx_led_set(struct led_classdev *led_cdev,
  53                             enum led_brightness value)
  54 {
  55         struct mc13xxx_led *led =
  56                 container_of(led_cdev, struct mc13xxx_led, cdev);
  57         struct mc13xxx_leds *leds = led->leds;
  58         unsigned int reg, bank, off, shift;
  59 
  60         switch (led->id) {
  61         case MC13783_LED_MD:
  62         case MC13783_LED_AD:
  63         case MC13783_LED_KP:
  64                 reg = 2;
  65                 shift = 9 + (led->id - MC13783_LED_MD) * 4;
  66                 break;
  67         case MC13783_LED_R1:
  68         case MC13783_LED_G1:
  69         case MC13783_LED_B1:
  70         case MC13783_LED_R2:
  71         case MC13783_LED_G2:
  72         case MC13783_LED_B2:
  73         case MC13783_LED_R3:
  74         case MC13783_LED_G3:
  75         case MC13783_LED_B3:
  76                 off = led->id - MC13783_LED_R1;
  77                 bank = off / 3;
  78                 reg = 3 + bank;
  79                 shift = (off - bank * 3) * 5 + 6;
  80                 break;
  81         case MC13892_LED_MD:
  82         case MC13892_LED_AD:
  83         case MC13892_LED_KP:
  84                 off = led->id - MC13892_LED_MD;
  85                 reg = off / 2;
  86                 shift = 3 + (off - reg * 2) * 12;
  87                 break;
  88         case MC13892_LED_R:
  89         case MC13892_LED_G:
  90         case MC13892_LED_B:
  91                 off = led->id - MC13892_LED_R;
  92                 bank = off / 2;
  93                 reg = 2 + bank;
  94                 shift = (off - bank * 2) * 12 + 3;
  95                 break;
  96         case MC34708_LED_R:
  97         case MC34708_LED_G:
  98                 reg = 0;
  99                 shift = 3 + (led->id - MC34708_LED_R) * 12;
 100                 break;
 101         default:
 102                 BUG();
 103         }
 104 
 105         return mc13xxx_reg_rmw(leds->master, leds->devtype->ledctrl_base + reg,
 106                         mc13xxx_max_brightness(led->id) << shift,
 107                         value << shift);
 108 }
 109 
 110 #ifdef CONFIG_OF
 111 static struct mc13xxx_leds_platform_data __init *mc13xxx_led_probe_dt(
 112         struct platform_device *pdev)
 113 {
 114         struct mc13xxx_leds *leds = platform_get_drvdata(pdev);
 115         struct mc13xxx_leds_platform_data *pdata;
 116         struct device_node *parent, *child;
 117         struct device *dev = &pdev->dev;
 118         int i = 0, ret = -ENODATA;
 119 
 120         pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
 121         if (!pdata)
 122                 return ERR_PTR(-ENOMEM);
 123 
 124         parent = of_get_child_by_name(dev->parent->of_node, "leds");
 125         if (!parent)
 126                 goto out_node_put;
 127 
 128         ret = of_property_read_u32_array(parent, "led-control",
 129                                          pdata->led_control,
 130                                          leds->devtype->num_regs);
 131         if (ret)
 132                 goto out_node_put;
 133 
 134         pdata->num_leds = of_get_child_count(parent);
 135 
 136         pdata->led = devm_kcalloc(dev, pdata->num_leds, sizeof(*pdata->led),
 137                                   GFP_KERNEL);
 138         if (!pdata->led) {
 139                 ret = -ENOMEM;
 140                 goto out_node_put;
 141         }
 142 
 143         for_each_child_of_node(parent, child) {
 144                 const char *str;
 145                 u32 tmp;
 146 
 147                 if (of_property_read_u32(child, "reg", &tmp))
 148                         continue;
 149                 pdata->led[i].id = leds->devtype->led_min + tmp;
 150 
 151                 if (!of_property_read_string(child, "label", &str))
 152                         pdata->led[i].name = str;
 153                 if (!of_property_read_string(child, "linux,default-trigger",
 154                                              &str))
 155                         pdata->led[i].default_trigger = str;
 156 
 157                 i++;
 158         }
 159 
 160         pdata->num_leds = i;
 161         ret = i > 0 ? 0 : -ENODATA;
 162 
 163 out_node_put:
 164         of_node_put(parent);
 165 
 166         return ret ? ERR_PTR(ret) : pdata;
 167 }
 168 #else
 169 static inline struct mc13xxx_leds_platform_data __init *mc13xxx_led_probe_dt(
 170         struct platform_device *pdev)
 171 {
 172         return ERR_PTR(-ENOSYS);
 173 }
 174 #endif
 175 
 176 static int __init mc13xxx_led_probe(struct platform_device *pdev)
 177 {
 178         struct device *dev = &pdev->dev;
 179         struct mc13xxx_leds_platform_data *pdata = dev_get_platdata(dev);
 180         struct mc13xxx *mcdev = dev_get_drvdata(dev->parent);
 181         struct mc13xxx_led_devtype *devtype =
 182                 (struct mc13xxx_led_devtype *)pdev->id_entry->driver_data;
 183         struct mc13xxx_leds *leds;
 184         int i, id, ret = -ENODATA;
 185         u32 init_led = 0;
 186 
 187         leds = devm_kzalloc(dev, sizeof(*leds), GFP_KERNEL);
 188         if (!leds)
 189                 return -ENOMEM;
 190 
 191         leds->devtype = devtype;
 192         leds->master = mcdev;
 193         platform_set_drvdata(pdev, leds);
 194 
 195         if (dev->parent->of_node) {
 196                 pdata = mc13xxx_led_probe_dt(pdev);
 197                 if (IS_ERR(pdata))
 198                         return PTR_ERR(pdata);
 199         } else if (!pdata)
 200                 return -ENODATA;
 201 
 202         leds->num_leds = pdata->num_leds;
 203 
 204         if ((leds->num_leds < 1) ||
 205             (leds->num_leds > (devtype->led_max - devtype->led_min + 1))) {
 206                 dev_err(dev, "Invalid LED count %d\n", leds->num_leds);
 207                 return -EINVAL;
 208         }
 209 
 210         leds->led = devm_kcalloc(dev, leds->num_leds, sizeof(*leds->led),
 211                                  GFP_KERNEL);
 212         if (!leds->led)
 213                 return -ENOMEM;
 214 
 215         for (i = 0; i < devtype->num_regs; i++) {
 216                 ret = mc13xxx_reg_write(mcdev, leds->devtype->ledctrl_base + i,
 217                                         pdata->led_control[i]);
 218                 if (ret)
 219                         return ret;
 220         }
 221 
 222         for (i = 0; i < leds->num_leds; i++) {
 223                 const char *name, *trig;
 224 
 225                 ret = -EINVAL;
 226 
 227                 id = pdata->led[i].id;
 228                 name = pdata->led[i].name;
 229                 trig = pdata->led[i].default_trigger;
 230 
 231                 if ((id > devtype->led_max) || (id < devtype->led_min)) {
 232                         dev_err(dev, "Invalid ID %i\n", id);
 233                         break;
 234                 }
 235 
 236                 if (init_led & (1 << id)) {
 237                         dev_warn(dev, "LED %i already initialized\n", id);
 238                         break;
 239                 }
 240 
 241                 init_led |= 1 << id;
 242                 leds->led[i].id = id;
 243                 leds->led[i].leds = leds;
 244                 leds->led[i].cdev.name = name;
 245                 leds->led[i].cdev.default_trigger = trig;
 246                 leds->led[i].cdev.flags = LED_CORE_SUSPENDRESUME;
 247                 leds->led[i].cdev.brightness_set_blocking = mc13xxx_led_set;
 248                 leds->led[i].cdev.max_brightness = mc13xxx_max_brightness(id);
 249 
 250                 ret = led_classdev_register(dev->parent, &leds->led[i].cdev);
 251                 if (ret) {
 252                         dev_err(dev, "Failed to register LED %i\n", id);
 253                         break;
 254                 }
 255         }
 256 
 257         if (ret)
 258                 while (--i >= 0)
 259                         led_classdev_unregister(&leds->led[i].cdev);
 260 
 261         return ret;
 262 }
 263 
 264 static int mc13xxx_led_remove(struct platform_device *pdev)
 265 {
 266         struct mc13xxx_leds *leds = platform_get_drvdata(pdev);
 267         int i;
 268 
 269         for (i = 0; i < leds->num_leds; i++)
 270                 led_classdev_unregister(&leds->led[i].cdev);
 271 
 272         return 0;
 273 }
 274 
 275 static const struct mc13xxx_led_devtype mc13783_led_devtype = {
 276         .led_min        = MC13783_LED_MD,
 277         .led_max        = MC13783_LED_B3,
 278         .num_regs       = 6,
 279         .ledctrl_base   = 51,
 280 };
 281 
 282 static const struct mc13xxx_led_devtype mc13892_led_devtype = {
 283         .led_min        = MC13892_LED_MD,
 284         .led_max        = MC13892_LED_B,
 285         .num_regs       = 4,
 286         .ledctrl_base   = 51,
 287 };
 288 
 289 static const struct mc13xxx_led_devtype mc34708_led_devtype = {
 290         .led_min        = MC34708_LED_R,
 291         .led_max        = MC34708_LED_G,
 292         .num_regs       = 1,
 293         .ledctrl_base   = 54,
 294 };
 295 
 296 static const struct platform_device_id mc13xxx_led_id_table[] = {
 297         { "mc13783-led", (kernel_ulong_t)&mc13783_led_devtype, },
 298         { "mc13892-led", (kernel_ulong_t)&mc13892_led_devtype, },
 299         { "mc34708-led", (kernel_ulong_t)&mc34708_led_devtype, },
 300         { }
 301 };
 302 MODULE_DEVICE_TABLE(platform, mc13xxx_led_id_table);
 303 
 304 static struct platform_driver mc13xxx_led_driver = {
 305         .driver = {
 306                 .name   = "mc13xxx-led",
 307         },
 308         .remove         = mc13xxx_led_remove,
 309         .id_table       = mc13xxx_led_id_table,
 310 };
 311 module_platform_driver_probe(mc13xxx_led_driver, mc13xxx_led_probe);
 312 
 313 MODULE_DESCRIPTION("LEDs driver for Freescale MC13XXX PMIC");
 314 MODULE_AUTHOR("Philippe Retornaz <philippe.retornaz@epfl.ch>");
 315 MODULE_LICENSE("GPL");

/* [<][>][^][v][top][bottom][index][help] */