linux/drivers/mfd/palmas.c
<<
>>
Prefs
   1/*
   2 * TI Palmas MFD Driver
   3 *
   4 * Copyright 2011-2012 Texas Instruments Inc.
   5 *
   6 * Author: Graeme Gregory <gg@slimlogic.co.uk>
   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
  15#include <linux/module.h>
  16#include <linux/moduleparam.h>
  17#include <linux/init.h>
  18#include <linux/slab.h>
  19#include <linux/i2c.h>
  20#include <linux/interrupt.h>
  21#include <linux/irq.h>
  22#include <linux/regmap.h>
  23#include <linux/err.h>
  24#include <linux/mfd/core.h>
  25#include <linux/mfd/palmas.h>
  26#include <linux/of_platform.h>
  27
  28enum palmas_ids {
  29        PALMAS_PMIC_ID,
  30        PALMAS_GPIO_ID,
  31        PALMAS_LEDS_ID,
  32        PALMAS_WDT_ID,
  33        PALMAS_RTC_ID,
  34        PALMAS_PWRBUTTON_ID,
  35        PALMAS_GPADC_ID,
  36        PALMAS_RESOURCE_ID,
  37        PALMAS_CLK_ID,
  38        PALMAS_PWM_ID,
  39        PALMAS_USB_ID,
  40};
  41
  42static struct resource palmas_rtc_resources[] = {
  43        {
  44                .start  = PALMAS_RTC_ALARM_IRQ,
  45                .end    = PALMAS_RTC_ALARM_IRQ,
  46                .flags  = IORESOURCE_IRQ,
  47        },
  48};
  49
  50static const struct mfd_cell palmas_children[] = {
  51        {
  52                .name = "palmas-pmic",
  53                .id = PALMAS_PMIC_ID,
  54        },
  55        {
  56                .name = "palmas-gpio",
  57                .id = PALMAS_GPIO_ID,
  58        },
  59        {
  60                .name = "palmas-leds",
  61                .id = PALMAS_LEDS_ID,
  62        },
  63        {
  64                .name = "palmas-wdt",
  65                .id = PALMAS_WDT_ID,
  66        },
  67        {
  68                .name = "palmas-rtc",
  69                .id = PALMAS_RTC_ID,
  70                .resources = &palmas_rtc_resources[0],
  71                .num_resources = ARRAY_SIZE(palmas_rtc_resources),
  72        },
  73        {
  74                .name = "palmas-pwrbutton",
  75                .id = PALMAS_PWRBUTTON_ID,
  76        },
  77        {
  78                .name = "palmas-gpadc",
  79                .id = PALMAS_GPADC_ID,
  80        },
  81        {
  82                .name = "palmas-resource",
  83                .id = PALMAS_RESOURCE_ID,
  84        },
  85        {
  86                .name = "palmas-clk",
  87                .id = PALMAS_CLK_ID,
  88        },
  89        {
  90                .name = "palmas-pwm",
  91                .id = PALMAS_PWM_ID,
  92        },
  93        {
  94                .name = "palmas-usb",
  95                .id = PALMAS_USB_ID,
  96        }
  97};
  98
  99static const struct regmap_config palmas_regmap_config[PALMAS_NUM_CLIENTS] = {
 100        {
 101                .reg_bits = 8,
 102                .val_bits = 8,
 103                .max_register = PALMAS_BASE_TO_REG(PALMAS_PU_PD_OD_BASE,
 104                                        PALMAS_PRIMARY_SECONDARY_PAD3),
 105        },
 106        {
 107                .reg_bits = 8,
 108                .val_bits = 8,
 109                .max_register = PALMAS_BASE_TO_REG(PALMAS_GPADC_BASE,
 110                                        PALMAS_GPADC_SMPS_VSEL_MONITORING),
 111        },
 112        {
 113                .reg_bits = 8,
 114                .val_bits = 8,
 115                .max_register = PALMAS_BASE_TO_REG(PALMAS_TRIM_GPADC_BASE,
 116                                        PALMAS_GPADC_TRIM16),
 117        },
 118};
 119
 120static const struct regmap_irq palmas_irqs[] = {
 121        /* INT1 IRQs */
 122        [PALMAS_CHARG_DET_N_VBUS_OVV_IRQ] = {
 123                .mask = PALMAS_INT1_STATUS_CHARG_DET_N_VBUS_OVV,
 124        },
 125        [PALMAS_PWRON_IRQ] = {
 126                .mask = PALMAS_INT1_STATUS_PWRON,
 127        },
 128        [PALMAS_LONG_PRESS_KEY_IRQ] = {
 129                .mask = PALMAS_INT1_STATUS_LONG_PRESS_KEY,
 130        },
 131        [PALMAS_RPWRON_IRQ] = {
 132                .mask = PALMAS_INT1_STATUS_RPWRON,
 133        },
 134        [PALMAS_PWRDOWN_IRQ] = {
 135                .mask = PALMAS_INT1_STATUS_PWRDOWN,
 136        },
 137        [PALMAS_HOTDIE_IRQ] = {
 138                .mask = PALMAS_INT1_STATUS_HOTDIE,
 139        },
 140        [PALMAS_VSYS_MON_IRQ] = {
 141                .mask = PALMAS_INT1_STATUS_VSYS_MON,
 142        },
 143        [PALMAS_VBAT_MON_IRQ] = {
 144                .mask = PALMAS_INT1_STATUS_VBAT_MON,
 145        },
 146        /* INT2 IRQs*/
 147        [PALMAS_RTC_ALARM_IRQ] = {
 148                .mask = PALMAS_INT2_STATUS_RTC_ALARM,
 149                .reg_offset = 1,
 150        },
 151        [PALMAS_RTC_TIMER_IRQ] = {
 152                .mask = PALMAS_INT2_STATUS_RTC_TIMER,
 153                .reg_offset = 1,
 154        },
 155        [PALMAS_WDT_IRQ] = {
 156                .mask = PALMAS_INT2_STATUS_WDT,
 157                .reg_offset = 1,
 158        },
 159        [PALMAS_BATREMOVAL_IRQ] = {
 160                .mask = PALMAS_INT2_STATUS_BATREMOVAL,
 161                .reg_offset = 1,
 162        },
 163        [PALMAS_RESET_IN_IRQ] = {
 164                .mask = PALMAS_INT2_STATUS_RESET_IN,
 165                .reg_offset = 1,
 166        },
 167        [PALMAS_FBI_BB_IRQ] = {
 168                .mask = PALMAS_INT2_STATUS_FBI_BB,
 169                .reg_offset = 1,
 170        },
 171        [PALMAS_SHORT_IRQ] = {
 172                .mask = PALMAS_INT2_STATUS_SHORT,
 173                .reg_offset = 1,
 174        },
 175        [PALMAS_VAC_ACOK_IRQ] = {
 176                .mask = PALMAS_INT2_STATUS_VAC_ACOK,
 177                .reg_offset = 1,
 178        },
 179        /* INT3 IRQs */
 180        [PALMAS_GPADC_AUTO_0_IRQ] = {
 181                .mask = PALMAS_INT3_STATUS_GPADC_AUTO_0,
 182                .reg_offset = 2,
 183        },
 184        [PALMAS_GPADC_AUTO_1_IRQ] = {
 185                .mask = PALMAS_INT3_STATUS_GPADC_AUTO_1,
 186                .reg_offset = 2,
 187        },
 188        [PALMAS_GPADC_EOC_SW_IRQ] = {
 189                .mask = PALMAS_INT3_STATUS_GPADC_EOC_SW,
 190                .reg_offset = 2,
 191        },
 192        [PALMAS_GPADC_EOC_RT_IRQ] = {
 193                .mask = PALMAS_INT3_STATUS_GPADC_EOC_RT,
 194                .reg_offset = 2,
 195        },
 196        [PALMAS_ID_OTG_IRQ] = {
 197                .mask = PALMAS_INT3_STATUS_ID_OTG,
 198                .reg_offset = 2,
 199        },
 200        [PALMAS_ID_IRQ] = {
 201                .mask = PALMAS_INT3_STATUS_ID,
 202                .reg_offset = 2,
 203        },
 204        [PALMAS_VBUS_OTG_IRQ] = {
 205                .mask = PALMAS_INT3_STATUS_VBUS_OTG,
 206                .reg_offset = 2,
 207        },
 208        [PALMAS_VBUS_IRQ] = {
 209                .mask = PALMAS_INT3_STATUS_VBUS,
 210                .reg_offset = 2,
 211        },
 212        /* INT4 IRQs */
 213        [PALMAS_GPIO_0_IRQ] = {
 214                .mask = PALMAS_INT4_STATUS_GPIO_0,
 215                .reg_offset = 3,
 216        },
 217        [PALMAS_GPIO_1_IRQ] = {
 218                .mask = PALMAS_INT4_STATUS_GPIO_1,
 219                .reg_offset = 3,
 220        },
 221        [PALMAS_GPIO_2_IRQ] = {
 222                .mask = PALMAS_INT4_STATUS_GPIO_2,
 223                .reg_offset = 3,
 224        },
 225        [PALMAS_GPIO_3_IRQ] = {
 226                .mask = PALMAS_INT4_STATUS_GPIO_3,
 227                .reg_offset = 3,
 228        },
 229        [PALMAS_GPIO_4_IRQ] = {
 230                .mask = PALMAS_INT4_STATUS_GPIO_4,
 231                .reg_offset = 3,
 232        },
 233        [PALMAS_GPIO_5_IRQ] = {
 234                .mask = PALMAS_INT4_STATUS_GPIO_5,
 235                .reg_offset = 3,
 236        },
 237        [PALMAS_GPIO_6_IRQ] = {
 238                .mask = PALMAS_INT4_STATUS_GPIO_6,
 239                .reg_offset = 3,
 240        },
 241        [PALMAS_GPIO_7_IRQ] = {
 242                .mask = PALMAS_INT4_STATUS_GPIO_7,
 243                .reg_offset = 3,
 244        },
 245};
 246
 247static struct regmap_irq_chip palmas_irq_chip = {
 248        .name = "palmas",
 249        .irqs = palmas_irqs,
 250        .num_irqs = ARRAY_SIZE(palmas_irqs),
 251
 252        .num_regs = 4,
 253        .irq_reg_stride = 5,
 254        .status_base = PALMAS_BASE_TO_REG(PALMAS_INTERRUPT_BASE,
 255                        PALMAS_INT1_STATUS),
 256        .mask_base = PALMAS_BASE_TO_REG(PALMAS_INTERRUPT_BASE,
 257                        PALMAS_INT1_MASK),
 258};
 259
 260static int palmas_set_pdata_irq_flag(struct i2c_client *i2c,
 261                struct palmas_platform_data *pdata)
 262{
 263        struct irq_data *irq_data = irq_get_irq_data(i2c->irq);
 264        if (!irq_data) {
 265                dev_err(&i2c->dev, "Invalid IRQ: %d\n", i2c->irq);
 266                return -EINVAL;
 267        }
 268
 269        pdata->irq_flags = irqd_get_trigger_type(irq_data);
 270        dev_info(&i2c->dev, "Irq flag is 0x%08x\n", pdata->irq_flags);
 271        return 0;
 272}
 273
 274static void palmas_dt_to_pdata(struct i2c_client *i2c,
 275                struct palmas_platform_data *pdata)
 276{
 277        struct device_node *node = i2c->dev.of_node;
 278        int ret;
 279        u32 prop;
 280
 281        ret = of_property_read_u32(node, "ti,mux_pad1", &prop);
 282        if (!ret) {
 283                pdata->mux_from_pdata = 1;
 284                pdata->pad1 = prop;
 285        }
 286
 287        ret = of_property_read_u32(node, "ti,mux_pad2", &prop);
 288        if (!ret) {
 289                pdata->mux_from_pdata = 1;
 290                pdata->pad2 = prop;
 291        }
 292
 293        /* The default for this register is all masked */
 294        ret = of_property_read_u32(node, "ti,power_ctrl", &prop);
 295        if (!ret)
 296                pdata->power_ctrl = prop;
 297        else
 298                pdata->power_ctrl = PALMAS_POWER_CTRL_NSLEEP_MASK |
 299                                        PALMAS_POWER_CTRL_ENABLE1_MASK |
 300                                        PALMAS_POWER_CTRL_ENABLE2_MASK;
 301        if (i2c->irq)
 302                palmas_set_pdata_irq_flag(i2c, pdata);
 303}
 304
 305static int palmas_i2c_probe(struct i2c_client *i2c,
 306                            const struct i2c_device_id *id)
 307{
 308        struct palmas *palmas;
 309        struct palmas_platform_data *pdata;
 310        struct device_node *node = i2c->dev.of_node;
 311        int ret = 0, i;
 312        unsigned int reg, addr;
 313        int slave;
 314        struct mfd_cell *children;
 315
 316        pdata = dev_get_platdata(&i2c->dev);
 317
 318        if (node && !pdata) {
 319                pdata = devm_kzalloc(&i2c->dev, sizeof(*pdata), GFP_KERNEL);
 320
 321                if (!pdata)
 322                        return -ENOMEM;
 323
 324                palmas_dt_to_pdata(i2c, pdata);
 325        }
 326
 327        if (!pdata)
 328                return -EINVAL;
 329
 330        palmas = devm_kzalloc(&i2c->dev, sizeof(struct palmas), GFP_KERNEL);
 331        if (palmas == NULL)
 332                return -ENOMEM;
 333
 334        i2c_set_clientdata(i2c, palmas);
 335        palmas->dev = &i2c->dev;
 336        palmas->id = id->driver_data;
 337        palmas->irq = i2c->irq;
 338
 339        for (i = 0; i < PALMAS_NUM_CLIENTS; i++) {
 340                if (i == 0)
 341                        palmas->i2c_clients[i] = i2c;
 342                else {
 343                        palmas->i2c_clients[i] =
 344                                        i2c_new_dummy(i2c->adapter,
 345                                                        i2c->addr + i);
 346                        if (!palmas->i2c_clients[i]) {
 347                                dev_err(palmas->dev,
 348                                        "can't attach client %d\n", i);
 349                                ret = -ENOMEM;
 350                                goto err;
 351                        }
 352                }
 353                palmas->regmap[i] = devm_regmap_init_i2c(palmas->i2c_clients[i],
 354                                &palmas_regmap_config[i]);
 355                if (IS_ERR(palmas->regmap[i])) {
 356                        ret = PTR_ERR(palmas->regmap[i]);
 357                        dev_err(palmas->dev,
 358                                "Failed to allocate regmap %d, err: %d\n",
 359                                i, ret);
 360                        goto err;
 361                }
 362        }
 363
 364        /* Change interrupt line output polarity */
 365        if (pdata->irq_flags & IRQ_TYPE_LEVEL_HIGH)
 366                reg = PALMAS_POLARITY_CTRL_INT_POLARITY;
 367        else
 368                reg = 0;
 369        ret = palmas_update_bits(palmas, PALMAS_PU_PD_OD_BASE,
 370                        PALMAS_POLARITY_CTRL, PALMAS_POLARITY_CTRL_INT_POLARITY,
 371                        reg);
 372        if (ret < 0) {
 373                dev_err(palmas->dev, "POLARITY_CTRL updat failed: %d\n", ret);
 374                goto err;
 375        }
 376
 377        /* Change IRQ into clear on read mode for efficiency */
 378        slave = PALMAS_BASE_TO_SLAVE(PALMAS_INTERRUPT_BASE);
 379        addr = PALMAS_BASE_TO_REG(PALMAS_INTERRUPT_BASE, PALMAS_INT_CTRL);
 380        reg = PALMAS_INT_CTRL_INT_CLEAR;
 381
 382        regmap_write(palmas->regmap[slave], addr, reg);
 383
 384        ret = regmap_add_irq_chip(palmas->regmap[slave], palmas->irq,
 385                        IRQF_ONESHOT | pdata->irq_flags, 0, &palmas_irq_chip,
 386                        &palmas->irq_data);
 387        if (ret < 0)
 388                goto err;
 389
 390        slave = PALMAS_BASE_TO_SLAVE(PALMAS_PU_PD_OD_BASE);
 391        addr = PALMAS_BASE_TO_REG(PALMAS_PU_PD_OD_BASE,
 392                        PALMAS_PRIMARY_SECONDARY_PAD1);
 393
 394        if (pdata->mux_from_pdata) {
 395                reg = pdata->pad1;
 396                ret = regmap_write(palmas->regmap[slave], addr, reg);
 397                if (ret)
 398                        goto err_irq;
 399        } else {
 400                ret = regmap_read(palmas->regmap[slave], addr, &reg);
 401                if (ret)
 402                        goto err_irq;
 403        }
 404
 405        if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_0))
 406                palmas->gpio_muxed |= PALMAS_GPIO_0_MUXED;
 407        if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_MASK))
 408                palmas->gpio_muxed |= PALMAS_GPIO_1_MUXED;
 409        else if ((reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_MASK) ==
 410                        (2 << PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_SHIFT))
 411                palmas->led_muxed |= PALMAS_LED1_MUXED;
 412        else if ((reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_MASK) ==
 413                        (3 << PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_SHIFT))
 414                palmas->pwm_muxed |= PALMAS_PWM1_MUXED;
 415        if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_2_MASK))
 416                palmas->gpio_muxed |= PALMAS_GPIO_2_MUXED;
 417        else if ((reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_2_MASK) ==
 418                        (2 << PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_2_SHIFT))
 419                palmas->led_muxed |= PALMAS_LED2_MUXED;
 420        else if ((reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_2_MASK) ==
 421                        (3 << PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_2_SHIFT))
 422                palmas->pwm_muxed |= PALMAS_PWM2_MUXED;
 423        if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_3))
 424                palmas->gpio_muxed |= PALMAS_GPIO_3_MUXED;
 425
 426        addr = PALMAS_BASE_TO_REG(PALMAS_PU_PD_OD_BASE,
 427                        PALMAS_PRIMARY_SECONDARY_PAD2);
 428
 429        if (pdata->mux_from_pdata) {
 430                reg = pdata->pad2;
 431                ret = regmap_write(palmas->regmap[slave], addr, reg);
 432                if (ret)
 433                        goto err_irq;
 434        } else {
 435                ret = regmap_read(palmas->regmap[slave], addr, &reg);
 436                if (ret)
 437                        goto err_irq;
 438        }
 439
 440        if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD2_GPIO_4))
 441                palmas->gpio_muxed |= PALMAS_GPIO_4_MUXED;
 442        if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD2_GPIO_5_MASK))
 443                palmas->gpio_muxed |= PALMAS_GPIO_5_MUXED;
 444        if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD2_GPIO_6))
 445                palmas->gpio_muxed |= PALMAS_GPIO_6_MUXED;
 446        if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD2_GPIO_7_MASK))
 447                palmas->gpio_muxed |= PALMAS_GPIO_7_MUXED;
 448
 449        dev_info(palmas->dev, "Muxing GPIO %x, PWM %x, LED %x\n",
 450                        palmas->gpio_muxed, palmas->pwm_muxed,
 451                        palmas->led_muxed);
 452
 453        reg = pdata->power_ctrl;
 454
 455        slave = PALMAS_BASE_TO_SLAVE(PALMAS_PMU_CONTROL_BASE);
 456        addr = PALMAS_BASE_TO_REG(PALMAS_PMU_CONTROL_BASE, PALMAS_POWER_CTRL);
 457
 458        ret = regmap_write(palmas->regmap[slave], addr, reg);
 459        if (ret)
 460                goto err_irq;
 461
 462        /*
 463         * If we are probing with DT do this the DT way and return here
 464         * otherwise continue and add devices using mfd helpers.
 465         */
 466        if (node) {
 467                ret = of_platform_populate(node, NULL, NULL, &i2c->dev);
 468                if (ret < 0)
 469                        goto err_irq;
 470                else
 471                        return ret;
 472        }
 473
 474        children = kmemdup(palmas_children, sizeof(palmas_children),
 475                           GFP_KERNEL);
 476        if (!children) {
 477                ret = -ENOMEM;
 478                goto err_irq;
 479        }
 480
 481        children[PALMAS_PMIC_ID].platform_data = pdata->pmic_pdata;
 482        children[PALMAS_PMIC_ID].pdata_size = sizeof(*pdata->pmic_pdata);
 483
 484        children[PALMAS_GPADC_ID].platform_data = pdata->gpadc_pdata;
 485        children[PALMAS_GPADC_ID].pdata_size = sizeof(*pdata->gpadc_pdata);
 486
 487        children[PALMAS_RESOURCE_ID].platform_data = pdata->resource_pdata;
 488        children[PALMAS_RESOURCE_ID].pdata_size =
 489                        sizeof(*pdata->resource_pdata);
 490
 491        children[PALMAS_USB_ID].platform_data = pdata->usb_pdata;
 492        children[PALMAS_USB_ID].pdata_size = sizeof(*pdata->usb_pdata);
 493
 494        children[PALMAS_CLK_ID].platform_data = pdata->clk_pdata;
 495        children[PALMAS_CLK_ID].pdata_size = sizeof(*pdata->clk_pdata);
 496
 497        ret = mfd_add_devices(palmas->dev, -1,
 498                              children, ARRAY_SIZE(palmas_children),
 499                              NULL, 0,
 500                              regmap_irq_get_domain(palmas->irq_data));
 501        kfree(children);
 502
 503        if (ret < 0)
 504                goto err_devices;
 505
 506        return ret;
 507
 508err_devices:
 509        mfd_remove_devices(palmas->dev);
 510err_irq:
 511        regmap_del_irq_chip(palmas->irq, palmas->irq_data);
 512err:
 513        return ret;
 514}
 515
 516static int palmas_i2c_remove(struct i2c_client *i2c)
 517{
 518        struct palmas *palmas = i2c_get_clientdata(i2c);
 519
 520        mfd_remove_devices(palmas->dev);
 521        regmap_del_irq_chip(palmas->irq, palmas->irq_data);
 522
 523        return 0;
 524}
 525
 526static const struct i2c_device_id palmas_i2c_id[] = {
 527        { "palmas", },
 528        { "twl6035", },
 529        { "twl6037", },
 530        { "tps65913", },
 531        { /* end */ }
 532};
 533MODULE_DEVICE_TABLE(i2c, palmas_i2c_id);
 534
 535static struct of_device_id of_palmas_match_tbl[] = {
 536        { .compatible = "ti,palmas", },
 537        { /* end */ }
 538};
 539
 540static struct i2c_driver palmas_i2c_driver = {
 541        .driver = {
 542                   .name = "palmas",
 543                   .of_match_table = of_palmas_match_tbl,
 544                   .owner = THIS_MODULE,
 545        },
 546        .probe = palmas_i2c_probe,
 547        .remove = palmas_i2c_remove,
 548        .id_table = palmas_i2c_id,
 549};
 550
 551static int __init palmas_i2c_init(void)
 552{
 553        return i2c_add_driver(&palmas_i2c_driver);
 554}
 555/* init early so consumer devices can complete system boot */
 556subsys_initcall(palmas_i2c_init);
 557
 558static void __exit palmas_i2c_exit(void)
 559{
 560        i2c_del_driver(&palmas_i2c_driver);
 561}
 562module_exit(palmas_i2c_exit);
 563
 564MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
 565MODULE_DESCRIPTION("Palmas chip family multi-function driver");
 566MODULE_LICENSE("GPL");
 567
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.