Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 1 | /* |
| 2 | * I2C driver for Marvell 88PM80x |
| 3 | * |
| 4 | * Copyright (C) 2012 Marvell International Ltd. |
| 5 | * Haojian Zhuang <haojian.zhuang@marvell.com> |
| 6 | * Joseph(Yossi) Hanin <yhanin@marvell.com> |
| 7 | * Qiao Zhou <zhouqiao@marvell.com> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | */ |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/i2c.h> |
| 16 | #include <linux/mfd/88pm80x.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/uaccess.h> |
| 19 | #include <linux/err.h> |
| 20 | |
Qiao Zhou | 5500e39 | 2012-07-09 14:37:33 +0800 | [diff] [blame^] | 21 | /* |
| 22 | * workaround: some registers needed by pm805 are defined in pm800, so |
| 23 | * need to use this global variable to maintain the relation between |
| 24 | * pm800 and pm805. would remove it after HW chip fixes the issue. |
| 25 | */ |
| 26 | static struct pm80x_chip *g_pm80x_chip; |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 27 | |
| 28 | const struct regmap_config pm80x_regmap_config = { |
| 29 | .reg_bits = 8, |
| 30 | .val_bits = 8, |
| 31 | }; |
| 32 | |
| 33 | int __devinit pm80x_init(struct i2c_client *client, |
| 34 | const struct i2c_device_id *id) |
| 35 | { |
| 36 | struct pm80x_chip *chip; |
| 37 | struct regmap *map; |
| 38 | int ret = 0; |
| 39 | |
| 40 | chip = |
| 41 | devm_kzalloc(&client->dev, sizeof(struct pm80x_chip), GFP_KERNEL); |
| 42 | if (!chip) |
| 43 | return -ENOMEM; |
| 44 | |
| 45 | map = devm_regmap_init_i2c(client, &pm80x_regmap_config); |
| 46 | if (IS_ERR(map)) { |
| 47 | ret = PTR_ERR(map); |
| 48 | dev_err(&client->dev, "Failed to allocate register map: %d\n", |
| 49 | ret); |
| 50 | goto err_regmap_init; |
| 51 | } |
| 52 | |
| 53 | chip->id = id->driver_data; |
| 54 | if (chip->id < CHIP_PM800 || chip->id > CHIP_PM805) { |
| 55 | ret = -EINVAL; |
| 56 | goto err_chip_id; |
| 57 | } |
| 58 | |
| 59 | chip->client = client; |
| 60 | chip->regmap = map; |
| 61 | |
| 62 | chip->irq = client->irq; |
| 63 | |
| 64 | chip->dev = &client->dev; |
| 65 | dev_set_drvdata(chip->dev, chip); |
| 66 | i2c_set_clientdata(chip->client, chip); |
| 67 | |
| 68 | device_init_wakeup(&client->dev, 1); |
| 69 | |
Qiao Zhou | 5500e39 | 2012-07-09 14:37:33 +0800 | [diff] [blame^] | 70 | /* |
| 71 | * workaround: set g_pm80x_chip to the first probed chip. if the |
| 72 | * second chip is probed, just point to the companion to each |
| 73 | * other so that pm805 can access those specific register. would |
| 74 | * remove it after HW chip fixes the issue. |
| 75 | */ |
| 76 | if (!g_pm80x_chip) |
| 77 | g_pm80x_chip = chip; |
| 78 | else { |
| 79 | chip->companion = g_pm80x_chip->client; |
| 80 | g_pm80x_chip->companion = chip->client; |
| 81 | } |
| 82 | |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 83 | return 0; |
| 84 | |
| 85 | err_chip_id: |
| 86 | regmap_exit(map); |
| 87 | err_regmap_init: |
| 88 | devm_kfree(&client->dev, chip); |
| 89 | return ret; |
| 90 | } |
| 91 | EXPORT_SYMBOL_GPL(pm80x_init); |
| 92 | |
| 93 | int __devexit pm80x_deinit(struct i2c_client *client) |
| 94 | { |
| 95 | struct pm80x_chip *chip = i2c_get_clientdata(client); |
| 96 | |
Qiao Zhou | 5500e39 | 2012-07-09 14:37:33 +0800 | [diff] [blame^] | 97 | /* |
| 98 | * workaround: clear the dependency between pm800 and pm805. |
| 99 | * would remove it after HW chip fixes the issue. |
| 100 | */ |
| 101 | if (g_pm80x_chip->companion) |
| 102 | g_pm80x_chip->companion = NULL; |
| 103 | else |
| 104 | g_pm80x_chip = NULL; |
| 105 | |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 106 | regmap_exit(chip->regmap); |
| 107 | devm_kfree(&client->dev, chip); |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | EXPORT_SYMBOL_GPL(pm80x_deinit); |
| 112 | |
| 113 | #ifdef CONFIG_PM_SLEEP |
| 114 | static int pm80x_suspend(struct device *dev) |
| 115 | { |
| 116 | struct i2c_client *client = container_of(dev, struct i2c_client, dev); |
| 117 | struct pm80x_chip *chip = i2c_get_clientdata(client); |
| 118 | |
| 119 | if (chip && chip->wu_flag) |
| 120 | if (device_may_wakeup(chip->dev)) |
| 121 | enable_irq_wake(chip->irq); |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | static int pm80x_resume(struct device *dev) |
| 127 | { |
| 128 | struct i2c_client *client = container_of(dev, struct i2c_client, dev); |
| 129 | struct pm80x_chip *chip = i2c_get_clientdata(client); |
| 130 | |
| 131 | if (chip && chip->wu_flag) |
| 132 | if (device_may_wakeup(chip->dev)) |
| 133 | disable_irq_wake(chip->irq); |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | #endif |
| 138 | |
| 139 | SIMPLE_DEV_PM_OPS(pm80x_pm_ops, pm80x_suspend, pm80x_resume); |
| 140 | EXPORT_SYMBOL_GPL(pm80x_pm_ops); |
| 141 | |
| 142 | MODULE_DESCRIPTION("I2C Driver for Marvell 88PM80x"); |
| 143 | MODULE_AUTHOR("Qiao Zhou <zhouqiao@marvell.com>"); |
| 144 | MODULE_LICENSE("GPL"); |