Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 1 | /* |
Joonyoung Shim | 2c7e6f5 | 2010-09-10 18:36:39 +0200 | [diff] [blame] | 2 | * max8998.c - mfd core driver for the Maxim 8998 |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2009-2010 Samsung Electronics |
| 5 | * Kyungmin Park <kyungmin.park@samsung.com> |
| 6 | * Marek Szyprowski <m.szyprowski@samsung.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 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/module.h> |
| 24 | #include <linux/moduleparam.h> |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/slab.h> |
| 27 | #include <linux/i2c.h> |
| 28 | #include <linux/mutex.h> |
| 29 | #include <linux/mfd/core.h> |
| 30 | #include <linux/mfd/max8998.h> |
| 31 | #include <linux/mfd/max8998-private.h> |
| 32 | |
Joonyoung Shim | 9b16c0a | 2010-08-06 11:28:08 +0900 | [diff] [blame] | 33 | #define RTC_I2C_ADDR (0x0c >> 1) |
| 34 | |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 35 | static struct mfd_cell max8998_devs[] = { |
| 36 | { |
| 37 | .name = "max8998-pmic", |
Joonyoung Shim | 9b16c0a | 2010-08-06 11:28:08 +0900 | [diff] [blame] | 38 | }, { |
| 39 | .name = "max8998-rtc", |
| 40 | }, |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 41 | }; |
| 42 | |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame] | 43 | int max8998_read_reg(struct i2c_client *i2c, u8 reg, u8 *dest) |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 44 | { |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame] | 45 | struct max8998_dev *max8998 = i2c_get_clientdata(i2c); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 46 | int ret; |
| 47 | |
| 48 | mutex_lock(&max8998->iolock); |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame] | 49 | ret = i2c_smbus_read_byte_data(i2c, reg); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 50 | mutex_unlock(&max8998->iolock); |
| 51 | if (ret < 0) |
| 52 | return ret; |
| 53 | |
| 54 | ret &= 0xff; |
| 55 | *dest = ret; |
| 56 | return 0; |
| 57 | } |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame] | 58 | EXPORT_SYMBOL(max8998_read_reg); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 59 | |
Joonyoung Shim | 2c7e6f5 | 2010-09-10 18:36:39 +0200 | [diff] [blame] | 60 | int max8998_bulk_read(struct i2c_client *i2c, u8 reg, int count, u8 *buf) |
| 61 | { |
| 62 | struct max8998_dev *max8998 = i2c_get_clientdata(i2c); |
| 63 | int ret; |
| 64 | |
| 65 | mutex_lock(&max8998->iolock); |
| 66 | ret = i2c_smbus_read_i2c_block_data(i2c, reg, count, buf); |
| 67 | mutex_unlock(&max8998->iolock); |
| 68 | if (ret < 0) |
| 69 | return ret; |
| 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | EXPORT_SYMBOL(max8998_bulk_read); |
| 74 | |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame] | 75 | int max8998_write_reg(struct i2c_client *i2c, u8 reg, u8 value) |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 76 | { |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame] | 77 | struct max8998_dev *max8998 = i2c_get_clientdata(i2c); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 78 | int ret; |
| 79 | |
| 80 | mutex_lock(&max8998->iolock); |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame] | 81 | ret = i2c_smbus_write_byte_data(i2c, reg, value); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 82 | mutex_unlock(&max8998->iolock); |
| 83 | return ret; |
| 84 | } |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame] | 85 | EXPORT_SYMBOL(max8998_write_reg); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 86 | |
Joonyoung Shim | 9b16c0a | 2010-08-06 11:28:08 +0900 | [diff] [blame] | 87 | int max8998_bulk_write(struct i2c_client *i2c, u8 reg, int count, u8 *buf) |
| 88 | { |
| 89 | struct max8998_dev *max8998 = i2c_get_clientdata(i2c); |
| 90 | int ret; |
| 91 | |
| 92 | mutex_lock(&max8998->iolock); |
| 93 | ret = i2c_smbus_write_i2c_block_data(i2c, reg, count, buf); |
| 94 | mutex_unlock(&max8998->iolock); |
| 95 | if (ret < 0) |
| 96 | return ret; |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | EXPORT_SYMBOL(max8998_bulk_write); |
| 101 | |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame] | 102 | int max8998_update_reg(struct i2c_client *i2c, u8 reg, u8 val, u8 mask) |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 103 | { |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame] | 104 | struct max8998_dev *max8998 = i2c_get_clientdata(i2c); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 105 | int ret; |
| 106 | |
| 107 | mutex_lock(&max8998->iolock); |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame] | 108 | ret = i2c_smbus_read_byte_data(i2c, reg); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 109 | if (ret >= 0) { |
| 110 | u8 old_val = ret & 0xff; |
| 111 | u8 new_val = (val & mask) | (old_val & (~mask)); |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame] | 112 | ret = i2c_smbus_write_byte_data(i2c, reg, new_val); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 113 | } |
| 114 | mutex_unlock(&max8998->iolock); |
| 115 | return ret; |
| 116 | } |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame] | 117 | EXPORT_SYMBOL(max8998_update_reg); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 118 | |
| 119 | static int max8998_i2c_probe(struct i2c_client *i2c, |
| 120 | const struct i2c_device_id *id) |
| 121 | { |
Joonyoung Shim | 2c7e6f5 | 2010-09-10 18:36:39 +0200 | [diff] [blame] | 122 | struct max8998_platform_data *pdata = i2c->dev.platform_data; |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 123 | struct max8998_dev *max8998; |
| 124 | int ret = 0; |
| 125 | |
| 126 | max8998 = kzalloc(sizeof(struct max8998_dev), GFP_KERNEL); |
Axel Lin | 8f1f151 | 2010-08-09 14:48:16 +0800 | [diff] [blame] | 127 | if (max8998 == NULL) |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 128 | return -ENOMEM; |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 129 | |
| 130 | i2c_set_clientdata(i2c, max8998); |
| 131 | max8998->dev = &i2c->dev; |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame] | 132 | max8998->i2c = i2c; |
Joonyoung Shim | 2c7e6f5 | 2010-09-10 18:36:39 +0200 | [diff] [blame] | 133 | max8998->irq = i2c->irq; |
Lukasz Majewski | 509bd47 | 2010-09-27 14:32:24 +0200 | [diff] [blame] | 134 | max8998->type = id->driver_data; |
Joonyoung Shim | 2c7e6f5 | 2010-09-10 18:36:39 +0200 | [diff] [blame] | 135 | if (pdata) { |
| 136 | max8998->ono = pdata->ono; |
| 137 | max8998->irq_base = pdata->irq_base; |
| 138 | } |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 139 | mutex_init(&max8998->iolock); |
| 140 | |
Joonyoung Shim | 9b16c0a | 2010-08-06 11:28:08 +0900 | [diff] [blame] | 141 | max8998->rtc = i2c_new_dummy(i2c->adapter, RTC_I2C_ADDR); |
| 142 | i2c_set_clientdata(max8998->rtc, max8998); |
| 143 | |
Joonyoung Shim | 2c7e6f5 | 2010-09-10 18:36:39 +0200 | [diff] [blame] | 144 | max8998_irq_init(max8998); |
| 145 | |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 146 | ret = mfd_add_devices(max8998->dev, -1, |
| 147 | max8998_devs, ARRAY_SIZE(max8998_devs), |
| 148 | NULL, 0); |
| 149 | if (ret < 0) |
| 150 | goto err; |
| 151 | |
| 152 | return ret; |
| 153 | |
| 154 | err: |
| 155 | mfd_remove_devices(max8998->dev); |
Axel Lin | 7484552 | 2010-10-22 08:31:49 +0800 | [diff] [blame] | 156 | max8998_irq_exit(max8998); |
| 157 | i2c_unregister_device(max8998->rtc); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 158 | kfree(max8998); |
| 159 | return ret; |
| 160 | } |
| 161 | |
| 162 | static int max8998_i2c_remove(struct i2c_client *i2c) |
| 163 | { |
| 164 | struct max8998_dev *max8998 = i2c_get_clientdata(i2c); |
| 165 | |
| 166 | mfd_remove_devices(max8998->dev); |
Axel Lin | 7484552 | 2010-10-22 08:31:49 +0800 | [diff] [blame] | 167 | max8998_irq_exit(max8998); |
| 168 | i2c_unregister_device(max8998->rtc); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 169 | kfree(max8998); |
| 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | static const struct i2c_device_id max8998_i2c_id[] = { |
Lukasz Majewski | 509bd47 | 2010-09-27 14:32:24 +0200 | [diff] [blame] | 175 | { "max8998", TYPE_MAX8998 }, |
| 176 | { "lp3974", TYPE_LP3974}, |
Kyungmin Park | f8539dd | 2010-08-23 13:46:49 +0900 | [diff] [blame] | 177 | { } |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 178 | }; |
| 179 | MODULE_DEVICE_TABLE(i2c, max8998_i2c_id); |
| 180 | |
| 181 | static struct i2c_driver max8998_i2c_driver = { |
| 182 | .driver = { |
| 183 | .name = "max8998", |
| 184 | .owner = THIS_MODULE, |
| 185 | }, |
| 186 | .probe = max8998_i2c_probe, |
| 187 | .remove = max8998_i2c_remove, |
| 188 | .id_table = max8998_i2c_id, |
| 189 | }; |
| 190 | |
| 191 | static int __init max8998_i2c_init(void) |
| 192 | { |
| 193 | return i2c_add_driver(&max8998_i2c_driver); |
| 194 | } |
| 195 | /* init early so consumer devices can complete system boot */ |
| 196 | subsys_initcall(max8998_i2c_init); |
| 197 | |
| 198 | static void __exit max8998_i2c_exit(void) |
| 199 | { |
| 200 | i2c_del_driver(&max8998_i2c_driver); |
| 201 | } |
| 202 | module_exit(max8998_i2c_exit); |
| 203 | |
| 204 | MODULE_DESCRIPTION("MAXIM 8998 multi-function core driver"); |
| 205 | MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>"); |
| 206 | MODULE_LICENSE("GPL"); |