Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Base driver for Marvell 88PM805 |
| 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 file is subject to the terms and conditions of the GNU General |
| 10 | * Public License. See the file "COPYING" in the main directory of this |
| 11 | * archive for more details. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | */ |
| 22 | |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/i2c.h> |
| 26 | #include <linux/irq.h> |
| 27 | #include <linux/mfd/core.h> |
| 28 | #include <linux/mfd/88pm80x.h> |
| 29 | #include <linux/slab.h> |
| 30 | #include <linux/delay.h> |
| 31 | |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 32 | static const struct i2c_device_id pm80x_id_table[] = { |
Chao Xie | 03dcc54 | 2013-06-14 01:21:51 -0400 | [diff] [blame] | 33 | {"88PM805", 0}, |
Samuel Ortiz | 31b3ffb | 2012-07-09 15:11:46 +0200 | [diff] [blame] | 34 | {} /* NULL terminated */ |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 35 | }; |
| 36 | MODULE_DEVICE_TABLE(i2c, pm80x_id_table); |
| 37 | |
| 38 | /* Interrupt Number in 88PM805 */ |
| 39 | enum { |
| 40 | PM805_IRQ_LDO_OFF, /*0 */ |
| 41 | PM805_IRQ_SRC_DPLL_LOCK, /*1 */ |
| 42 | PM805_IRQ_CLIP_FAULT, |
| 43 | PM805_IRQ_MIC_CONFLICT, |
| 44 | PM805_IRQ_HP2_SHRT, |
| 45 | PM805_IRQ_HP1_SHRT, /*5 */ |
| 46 | PM805_IRQ_FINE_PLL_FAULT, |
| 47 | PM805_IRQ_RAW_PLL_FAULT, |
| 48 | PM805_IRQ_VOLP_BTN_DET, |
| 49 | PM805_IRQ_VOLM_BTN_DET, |
| 50 | PM805_IRQ_SHRT_BTN_DET, /*10 */ |
| 51 | PM805_IRQ_MIC_DET, /*11 */ |
| 52 | |
| 53 | PM805_MAX_IRQ, |
| 54 | }; |
| 55 | |
| 56 | static struct resource codec_resources[] = { |
Zhen Lei | ba4672ad | 2021-06-01 15:04:25 +0800 | [diff] [blame] | 57 | /* Headset microphone insertion or removal */ |
| 58 | DEFINE_RES_IRQ_NAMED(PM805_IRQ_MIC_DET, "micin"), |
| 59 | |
| 60 | /* Audio short HP1 */ |
| 61 | DEFINE_RES_IRQ_NAMED(PM805_IRQ_HP1_SHRT, "audio-short1"), |
| 62 | |
| 63 | /* Audio short HP2 */ |
| 64 | DEFINE_RES_IRQ_NAMED(PM805_IRQ_HP2_SHRT, "audio-short2"), |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 65 | }; |
| 66 | |
Geert Uytterhoeven | 04e0241 | 2013-11-18 14:32:59 +0100 | [diff] [blame] | 67 | static const struct mfd_cell codec_devs[] = { |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 68 | { |
| 69 | .name = "88pm80x-codec", |
| 70 | .num_resources = ARRAY_SIZE(codec_resources), |
| 71 | .resources = &codec_resources[0], |
| 72 | .id = -1, |
| 73 | }, |
| 74 | }; |
| 75 | |
| 76 | static struct regmap_irq pm805_irqs[] = { |
| 77 | /* INT0 */ |
| 78 | [PM805_IRQ_LDO_OFF] = { |
| 79 | .mask = PM805_INT1_HP1_SHRT, |
| 80 | }, |
| 81 | [PM805_IRQ_SRC_DPLL_LOCK] = { |
| 82 | .mask = PM805_INT1_HP2_SHRT, |
| 83 | }, |
| 84 | [PM805_IRQ_CLIP_FAULT] = { |
| 85 | .mask = PM805_INT1_MIC_CONFLICT, |
| 86 | }, |
| 87 | [PM805_IRQ_MIC_CONFLICT] = { |
| 88 | .mask = PM805_INT1_CLIP_FAULT, |
| 89 | }, |
| 90 | [PM805_IRQ_HP2_SHRT] = { |
| 91 | .mask = PM805_INT1_LDO_OFF, |
| 92 | }, |
| 93 | [PM805_IRQ_HP1_SHRT] = { |
| 94 | .mask = PM805_INT1_SRC_DPLL_LOCK, |
| 95 | }, |
| 96 | /* INT1 */ |
| 97 | [PM805_IRQ_FINE_PLL_FAULT] = { |
| 98 | .reg_offset = 1, |
| 99 | .mask = PM805_INT2_MIC_DET, |
| 100 | }, |
| 101 | [PM805_IRQ_RAW_PLL_FAULT] = { |
| 102 | .reg_offset = 1, |
| 103 | .mask = PM805_INT2_SHRT_BTN_DET, |
| 104 | }, |
| 105 | [PM805_IRQ_VOLP_BTN_DET] = { |
| 106 | .reg_offset = 1, |
| 107 | .mask = PM805_INT2_VOLM_BTN_DET, |
| 108 | }, |
| 109 | [PM805_IRQ_VOLM_BTN_DET] = { |
| 110 | .reg_offset = 1, |
| 111 | .mask = PM805_INT2_VOLP_BTN_DET, |
| 112 | }, |
| 113 | [PM805_IRQ_SHRT_BTN_DET] = { |
| 114 | .reg_offset = 1, |
| 115 | .mask = PM805_INT2_RAW_PLL_FAULT, |
| 116 | }, |
| 117 | [PM805_IRQ_MIC_DET] = { |
| 118 | .reg_offset = 1, |
| 119 | .mask = PM805_INT2_FINE_PLL_FAULT, |
| 120 | }, |
| 121 | }; |
| 122 | |
Bill Pemberton | f791be4 | 2012-11-19 13:23:04 -0500 | [diff] [blame] | 123 | static int device_irq_init_805(struct pm80x_chip *chip) |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 124 | { |
| 125 | struct regmap *map = chip->regmap; |
Yi Zhang | 1ef5677 | 2013-06-14 01:21:48 -0400 | [diff] [blame] | 126 | unsigned long flags = IRQF_ONESHOT; |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 127 | int data, mask, ret = -EINVAL; |
| 128 | |
| 129 | if (!map || !chip->irq) { |
| 130 | dev_err(chip->dev, "incorrect parameters\n"); |
| 131 | return -EINVAL; |
| 132 | } |
| 133 | |
| 134 | /* |
| 135 | * irq_mode defines the way of clearing interrupt. it's read-clear by |
| 136 | * default. |
| 137 | */ |
| 138 | mask = |
| 139 | PM805_STATUS0_INT_CLEAR | PM805_STATUS0_INV_INT | |
| 140 | PM800_STATUS0_INT_MASK; |
| 141 | |
| 142 | data = PM805_STATUS0_INT_CLEAR; |
| 143 | ret = regmap_update_bits(map, PM805_INT_STATUS0, mask, data); |
| 144 | /* |
| 145 | * PM805_INT_STATUS is under 32K clock domain, so need to |
| 146 | * add proper delay before the next I2C register access. |
| 147 | */ |
Lee Jones | dc54392 | 2014-05-08 12:06:19 +0100 | [diff] [blame] | 148 | usleep_range(1000, 3000); |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 149 | |
| 150 | if (ret < 0) |
| 151 | goto out; |
| 152 | |
| 153 | ret = |
| 154 | regmap_add_irq_chip(chip->regmap, chip->irq, flags, -1, |
| 155 | chip->regmap_irq_chip, &chip->irq_data); |
| 156 | |
| 157 | out: |
| 158 | return ret; |
| 159 | } |
| 160 | |
| 161 | static void device_irq_exit_805(struct pm80x_chip *chip) |
| 162 | { |
| 163 | regmap_del_irq_chip(chip->irq, chip->irq_data); |
| 164 | } |
| 165 | |
| 166 | static struct regmap_irq_chip pm805_irq_chip = { |
| 167 | .name = "88pm805", |
| 168 | .irqs = pm805_irqs, |
| 169 | .num_irqs = ARRAY_SIZE(pm805_irqs), |
| 170 | |
| 171 | .num_regs = 2, |
| 172 | .status_base = PM805_INT_STATUS1, |
| 173 | .mask_base = PM805_INT_MASK1, |
| 174 | .ack_base = PM805_INT_STATUS1, |
| 175 | }; |
| 176 | |
Bill Pemberton | f791be4 | 2012-11-19 13:23:04 -0500 | [diff] [blame] | 177 | static int device_805_init(struct pm80x_chip *chip) |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 178 | { |
| 179 | int ret = 0; |
| 180 | struct regmap *map = chip->regmap; |
| 181 | |
| 182 | if (!map) { |
| 183 | dev_err(chip->dev, "regmap is invalid\n"); |
| 184 | return -EINVAL; |
| 185 | } |
| 186 | |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 187 | chip->regmap_irq_chip = &pm805_irq_chip; |
| 188 | |
| 189 | ret = device_irq_init_805(chip); |
| 190 | if (ret < 0) { |
| 191 | dev_err(chip->dev, "Failed to init pm805 irq!\n"); |
| 192 | goto out_irq_init; |
| 193 | } |
| 194 | |
| 195 | ret = mfd_add_devices(chip->dev, 0, &codec_devs[0], |
Mark Brown | 0848c94 | 2012-09-11 15:16:36 +0800 | [diff] [blame] | 196 | ARRAY_SIZE(codec_devs), &codec_resources[0], 0, |
| 197 | NULL); |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 198 | if (ret < 0) { |
| 199 | dev_err(chip->dev, "Failed to add codec subdev\n"); |
| 200 | goto out_codec; |
| 201 | } else |
| 202 | dev_info(chip->dev, "[%s]:Added mfd codec_devs\n", __func__); |
| 203 | |
| 204 | return 0; |
| 205 | |
| 206 | out_codec: |
| 207 | device_irq_exit_805(chip); |
| 208 | out_irq_init: |
| 209 | return ret; |
| 210 | } |
| 211 | |
Bill Pemberton | f791be4 | 2012-11-19 13:23:04 -0500 | [diff] [blame] | 212 | static int pm805_probe(struct i2c_client *client, |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 213 | const struct i2c_device_id *id) |
| 214 | { |
| 215 | int ret = 0; |
| 216 | struct pm80x_chip *chip; |
Jingoo Han | 334a41ce | 2013-07-30 17:10:05 +0900 | [diff] [blame] | 217 | struct pm80x_platform_data *pdata = dev_get_platdata(&client->dev); |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 218 | |
Chao Xie | 03dcc54 | 2013-06-14 01:21:51 -0400 | [diff] [blame] | 219 | ret = pm80x_init(client); |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 220 | if (ret) { |
| 221 | dev_err(&client->dev, "pm805_init fail!\n"); |
| 222 | goto out_init; |
| 223 | } |
| 224 | |
| 225 | chip = i2c_get_clientdata(client); |
| 226 | |
| 227 | ret = device_805_init(chip); |
| 228 | if (ret) { |
Chao Xie | 03dcc54 | 2013-06-14 01:21:51 -0400 | [diff] [blame] | 229 | dev_err(chip->dev, "Failed to initialize 88pm805 devices\n"); |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 230 | goto err_805_init; |
| 231 | } |
| 232 | |
Chao Xie | 2b274fe | 2013-08-18 21:27:55 -0400 | [diff] [blame] | 233 | if (pdata && pdata->plat_config) |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 234 | pdata->plat_config(chip, pdata); |
| 235 | |
| 236 | err_805_init: |
Yi Zhang | 306df79 | 2013-01-22 10:43:45 +0800 | [diff] [blame] | 237 | pm80x_deinit(); |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 238 | out_init: |
| 239 | return ret; |
| 240 | } |
| 241 | |
Bill Pemberton | 4740f73 | 2012-11-19 13:26:01 -0500 | [diff] [blame] | 242 | static int pm805_remove(struct i2c_client *client) |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 243 | { |
| 244 | struct pm80x_chip *chip = i2c_get_clientdata(client); |
| 245 | |
| 246 | mfd_remove_devices(chip->dev); |
| 247 | device_irq_exit_805(chip); |
| 248 | |
Yi Zhang | 306df79 | 2013-01-22 10:43:45 +0800 | [diff] [blame] | 249 | pm80x_deinit(); |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 250 | |
| 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | static struct i2c_driver pm805_driver = { |
| 255 | .driver = { |
Chao Xie | 46223a1 | 2013-06-14 01:21:46 -0400 | [diff] [blame] | 256 | .name = "88PM805", |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 257 | .pm = &pm80x_pm_ops, |
| 258 | }, |
| 259 | .probe = pm805_probe, |
Bill Pemberton | 8444921 | 2012-11-19 13:20:24 -0500 | [diff] [blame] | 260 | .remove = pm805_remove, |
Qiao Zhou | 70c6cce | 2012-07-09 14:37:32 +0800 | [diff] [blame] | 261 | .id_table = pm80x_id_table, |
| 262 | }; |
| 263 | |
| 264 | static int __init pm805_i2c_init(void) |
| 265 | { |
| 266 | return i2c_add_driver(&pm805_driver); |
| 267 | } |
| 268 | subsys_initcall(pm805_i2c_init); |
| 269 | |
| 270 | static void __exit pm805_i2c_exit(void) |
| 271 | { |
| 272 | i2c_del_driver(&pm805_driver); |
| 273 | } |
| 274 | module_exit(pm805_i2c_exit); |
| 275 | |
| 276 | MODULE_DESCRIPTION("PMIC Driver for Marvell 88PM805"); |
| 277 | MODULE_AUTHOR("Qiao Zhou <zhouqiao@marvell.com>"); |
| 278 | MODULE_LICENSE("GPL"); |