Sangbeom Kim | 0f5f707 | 2011-12-23 17:28:08 +0900 | [diff] [blame] | 1 | /* |
| 2 | * s5m87xx.c |
| 3 | * |
| 4 | * Copyright (c) 2011 Samsung Electronics Co., Ltd |
| 5 | * http://www.samsung.com |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify it |
| 8 | * under the terms of the GNU General Public License as published by the |
| 9 | * Free Software Foundation; either version 2 of the License, or (at your |
| 10 | * option) any later version. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/moduleparam.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/err.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/i2c.h> |
| 20 | #include <linux/interrupt.h> |
| 21 | #include <linux/pm_runtime.h> |
| 22 | #include <linux/mutex.h> |
| 23 | #include <linux/mfd/core.h> |
| 24 | #include <linux/mfd/s5m87xx/s5m-core.h> |
| 25 | #include <linux/mfd/s5m87xx/s5m-pmic.h> |
| 26 | #include <linux/mfd/s5m87xx/s5m-rtc.h> |
| 27 | #include <linux/regmap.h> |
| 28 | |
Sangbeom Kim | 88a1cfd | 2012-01-20 16:09:12 +0900 | [diff] [blame] | 29 | static struct mfd_cell s5m8751_devs[] = { |
| 30 | { |
| 31 | .name = "s5m8751-pmic", |
| 32 | }, { |
| 33 | .name = "s5m-charger", |
| 34 | }, { |
| 35 | .name = "s5m8751-codec", |
| 36 | }, |
| 37 | }; |
| 38 | |
| 39 | static struct mfd_cell s5m8763_devs[] = { |
| 40 | { |
| 41 | .name = "s5m8763-pmic", |
| 42 | }, { |
| 43 | .name = "s5m-rtc", |
| 44 | }, { |
| 45 | .name = "s5m-charger", |
| 46 | }, |
| 47 | }; |
| 48 | |
| 49 | static struct mfd_cell s5m8767_devs[] = { |
Sangbeom Kim | 0f5f707 | 2011-12-23 17:28:08 +0900 | [diff] [blame] | 50 | { |
| 51 | .name = "s5m8767-pmic", |
| 52 | }, { |
| 53 | .name = "s5m-rtc", |
| 54 | }, |
| 55 | }; |
| 56 | |
| 57 | int s5m_reg_read(struct s5m87xx_dev *s5m87xx, u8 reg, void *dest) |
| 58 | { |
| 59 | return regmap_read(s5m87xx->regmap, reg, dest); |
| 60 | } |
| 61 | EXPORT_SYMBOL_GPL(s5m_reg_read); |
| 62 | |
| 63 | int s5m_bulk_read(struct s5m87xx_dev *s5m87xx, u8 reg, int count, u8 *buf) |
| 64 | { |
Axel Lin | 61a2af3 | 2012-02-28 14:23:55 +0800 | [diff] [blame] | 65 | return regmap_bulk_read(s5m87xx->regmap, reg, buf, count); |
Sangbeom Kim | 0f5f707 | 2011-12-23 17:28:08 +0900 | [diff] [blame] | 66 | } |
| 67 | EXPORT_SYMBOL_GPL(s5m_bulk_read); |
| 68 | |
| 69 | int s5m_reg_write(struct s5m87xx_dev *s5m87xx, u8 reg, u8 value) |
| 70 | { |
| 71 | return regmap_write(s5m87xx->regmap, reg, value); |
| 72 | } |
| 73 | EXPORT_SYMBOL_GPL(s5m_reg_write); |
| 74 | |
| 75 | int s5m_bulk_write(struct s5m87xx_dev *s5m87xx, u8 reg, int count, u8 *buf) |
| 76 | { |
Axel Lin | 113351b | 2012-02-14 11:08:07 +0800 | [diff] [blame] | 77 | return regmap_raw_write(s5m87xx->regmap, reg, buf, count); |
Sangbeom Kim | 0f5f707 | 2011-12-23 17:28:08 +0900 | [diff] [blame] | 78 | } |
| 79 | EXPORT_SYMBOL_GPL(s5m_bulk_write); |
| 80 | |
| 81 | int s5m_reg_update(struct s5m87xx_dev *s5m87xx, u8 reg, u8 val, u8 mask) |
| 82 | { |
| 83 | return regmap_update_bits(s5m87xx->regmap, reg, mask, val); |
| 84 | } |
| 85 | EXPORT_SYMBOL_GPL(s5m_reg_update); |
| 86 | |
| 87 | static struct regmap_config s5m_regmap_config = { |
| 88 | .reg_bits = 8, |
| 89 | .val_bits = 8, |
| 90 | }; |
| 91 | |
| 92 | static int s5m87xx_i2c_probe(struct i2c_client *i2c, |
| 93 | const struct i2c_device_id *id) |
| 94 | { |
| 95 | struct s5m_platform_data *pdata = i2c->dev.platform_data; |
| 96 | struct s5m87xx_dev *s5m87xx; |
Axel Lin | 61a2af3 | 2012-02-28 14:23:55 +0800 | [diff] [blame] | 97 | int ret; |
Sangbeom Kim | 0f5f707 | 2011-12-23 17:28:08 +0900 | [diff] [blame] | 98 | |
Sangbeom Kim | 621210e | 2012-01-20 16:09:11 +0900 | [diff] [blame] | 99 | s5m87xx = devm_kzalloc(&i2c->dev, sizeof(struct s5m87xx_dev), |
| 100 | GFP_KERNEL); |
Sangbeom Kim | 0f5f707 | 2011-12-23 17:28:08 +0900 | [diff] [blame] | 101 | if (s5m87xx == NULL) |
| 102 | return -ENOMEM; |
| 103 | |
| 104 | i2c_set_clientdata(i2c, s5m87xx); |
| 105 | s5m87xx->dev = &i2c->dev; |
| 106 | s5m87xx->i2c = i2c; |
| 107 | s5m87xx->irq = i2c->irq; |
| 108 | s5m87xx->type = id->driver_data; |
| 109 | |
| 110 | if (pdata) { |
| 111 | s5m87xx->device_type = pdata->device_type; |
| 112 | s5m87xx->ono = pdata->ono; |
| 113 | s5m87xx->irq_base = pdata->irq_base; |
| 114 | s5m87xx->wakeup = pdata->wakeup; |
| 115 | } |
| 116 | |
Axel Lin | 1092e1c | 2012-04-25 10:03:44 +0800 | [diff] [blame] | 117 | s5m87xx->regmap = devm_regmap_init_i2c(i2c, &s5m_regmap_config); |
Sangbeom Kim | 0f5f707 | 2011-12-23 17:28:08 +0900 | [diff] [blame] | 118 | if (IS_ERR(s5m87xx->regmap)) { |
Axel Lin | 61a2af3 | 2012-02-28 14:23:55 +0800 | [diff] [blame] | 119 | ret = PTR_ERR(s5m87xx->regmap); |
Sangbeom Kim | 0f5f707 | 2011-12-23 17:28:08 +0900 | [diff] [blame] | 120 | dev_err(&i2c->dev, "Failed to allocate register map: %d\n", |
Axel Lin | 61a2af3 | 2012-02-28 14:23:55 +0800 | [diff] [blame] | 121 | ret); |
Axel Lin | 1092e1c | 2012-04-25 10:03:44 +0800 | [diff] [blame] | 122 | return ret; |
Sangbeom Kim | 0f5f707 | 2011-12-23 17:28:08 +0900 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | s5m87xx->rtc = i2c_new_dummy(i2c->adapter, RTC_I2C_ADDR); |
| 126 | i2c_set_clientdata(s5m87xx->rtc, s5m87xx); |
| 127 | |
Jonghwan Choi | c3ebb30 | 2012-01-16 09:08:42 +0900 | [diff] [blame] | 128 | if (pdata && pdata->cfg_pmic_irq) |
Sangbeom Kim | 0f5f707 | 2011-12-23 17:28:08 +0900 | [diff] [blame] | 129 | pdata->cfg_pmic_irq(); |
| 130 | |
| 131 | s5m_irq_init(s5m87xx); |
| 132 | |
| 133 | pm_runtime_set_active(s5m87xx->dev); |
| 134 | |
Sangbeom Kim | 88a1cfd | 2012-01-20 16:09:12 +0900 | [diff] [blame] | 135 | switch (s5m87xx->device_type) { |
| 136 | case S5M8751X: |
| 137 | ret = mfd_add_devices(s5m87xx->dev, -1, s5m8751_devs, |
| 138 | ARRAY_SIZE(s5m8751_devs), NULL, 0); |
| 139 | break; |
| 140 | case S5M8763X: |
| 141 | ret = mfd_add_devices(s5m87xx->dev, -1, s5m8763_devs, |
| 142 | ARRAY_SIZE(s5m8763_devs), NULL, 0); |
| 143 | break; |
| 144 | case S5M8767X: |
| 145 | ret = mfd_add_devices(s5m87xx->dev, -1, s5m8767_devs, |
| 146 | ARRAY_SIZE(s5m8767_devs), NULL, 0); |
| 147 | break; |
| 148 | default: |
| 149 | /* If this happens the probe function is problem */ |
| 150 | BUG(); |
| 151 | } |
Sangbeom Kim | 0f5f707 | 2011-12-23 17:28:08 +0900 | [diff] [blame] | 152 | |
| 153 | if (ret < 0) |
| 154 | goto err; |
| 155 | |
| 156 | return ret; |
| 157 | |
| 158 | err: |
| 159 | mfd_remove_devices(s5m87xx->dev); |
| 160 | s5m_irq_exit(s5m87xx); |
| 161 | i2c_unregister_device(s5m87xx->rtc); |
Sangbeom Kim | 0f5f707 | 2011-12-23 17:28:08 +0900 | [diff] [blame] | 162 | return ret; |
| 163 | } |
| 164 | |
| 165 | static int s5m87xx_i2c_remove(struct i2c_client *i2c) |
| 166 | { |
| 167 | struct s5m87xx_dev *s5m87xx = i2c_get_clientdata(i2c); |
| 168 | |
| 169 | mfd_remove_devices(s5m87xx->dev); |
| 170 | s5m_irq_exit(s5m87xx); |
| 171 | i2c_unregister_device(s5m87xx->rtc); |
Sangbeom Kim | 0f5f707 | 2011-12-23 17:28:08 +0900 | [diff] [blame] | 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | static const struct i2c_device_id s5m87xx_i2c_id[] = { |
| 176 | { "s5m87xx", 0 }, |
| 177 | { } |
| 178 | }; |
| 179 | MODULE_DEVICE_TABLE(i2c, s5m87xx_i2c_id); |
| 180 | |
| 181 | static struct i2c_driver s5m87xx_i2c_driver = { |
| 182 | .driver = { |
| 183 | .name = "s5m87xx", |
| 184 | .owner = THIS_MODULE, |
| 185 | }, |
| 186 | .probe = s5m87xx_i2c_probe, |
| 187 | .remove = s5m87xx_i2c_remove, |
| 188 | .id_table = s5m87xx_i2c_id, |
| 189 | }; |
| 190 | |
| 191 | static int __init s5m87xx_i2c_init(void) |
| 192 | { |
| 193 | return i2c_add_driver(&s5m87xx_i2c_driver); |
| 194 | } |
| 195 | |
| 196 | subsys_initcall(s5m87xx_i2c_init); |
| 197 | |
| 198 | static void __exit s5m87xx_i2c_exit(void) |
| 199 | { |
| 200 | i2c_del_driver(&s5m87xx_i2c_driver); |
| 201 | } |
| 202 | module_exit(s5m87xx_i2c_exit); |
| 203 | |
| 204 | MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>"); |
| 205 | MODULE_DESCRIPTION("Core support for the S5M MFD"); |
| 206 | MODULE_LICENSE("GPL"); |