Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 1 | /* |
| 2 | * max8698.c - mfd core driver for the Maxim 8998 |
| 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 | |
| 33 | static struct mfd_cell max8998_devs[] = { |
| 34 | { |
| 35 | .name = "max8998-pmic", |
| 36 | } |
| 37 | }; |
| 38 | |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame^] | 39 | int max8998_read_reg(struct i2c_client *i2c, u8 reg, u8 *dest) |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 40 | { |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame^] | 41 | struct max8998_dev *max8998 = i2c_get_clientdata(i2c); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 42 | int ret; |
| 43 | |
| 44 | mutex_lock(&max8998->iolock); |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame^] | 45 | ret = i2c_smbus_read_byte_data(i2c, reg); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 46 | mutex_unlock(&max8998->iolock); |
| 47 | if (ret < 0) |
| 48 | return ret; |
| 49 | |
| 50 | ret &= 0xff; |
| 51 | *dest = ret; |
| 52 | return 0; |
| 53 | } |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame^] | 54 | EXPORT_SYMBOL(max8998_read_reg); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 55 | |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame^] | 56 | int max8998_write_reg(struct i2c_client *i2c, u8 reg, u8 value) |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 57 | { |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame^] | 58 | struct max8998_dev *max8998 = i2c_get_clientdata(i2c); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 59 | int ret; |
| 60 | |
| 61 | mutex_lock(&max8998->iolock); |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame^] | 62 | ret = i2c_smbus_write_byte_data(i2c, reg, value); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 63 | mutex_unlock(&max8998->iolock); |
| 64 | return ret; |
| 65 | } |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame^] | 66 | EXPORT_SYMBOL(max8998_write_reg); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 67 | |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame^] | 68 | 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] | 69 | { |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame^] | 70 | struct max8998_dev *max8998 = i2c_get_clientdata(i2c); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 71 | int ret; |
| 72 | |
| 73 | mutex_lock(&max8998->iolock); |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame^] | 74 | ret = i2c_smbus_read_byte_data(i2c, reg); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 75 | if (ret >= 0) { |
| 76 | u8 old_val = ret & 0xff; |
| 77 | u8 new_val = (val & mask) | (old_val & (~mask)); |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame^] | 78 | ret = i2c_smbus_write_byte_data(i2c, reg, new_val); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 79 | if (ret >= 0) |
| 80 | ret = 0; |
| 81 | } |
| 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_update_reg); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 86 | |
| 87 | static int max8998_i2c_probe(struct i2c_client *i2c, |
| 88 | const struct i2c_device_id *id) |
| 89 | { |
| 90 | struct max8998_dev *max8998; |
| 91 | int ret = 0; |
| 92 | |
| 93 | max8998 = kzalloc(sizeof(struct max8998_dev), GFP_KERNEL); |
Axel Lin | 8f1f151 | 2010-08-09 14:48:16 +0800 | [diff] [blame] | 94 | if (max8998 == NULL) |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 95 | return -ENOMEM; |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 96 | |
| 97 | i2c_set_clientdata(i2c, max8998); |
| 98 | max8998->dev = &i2c->dev; |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame^] | 99 | max8998->i2c = i2c; |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 100 | mutex_init(&max8998->iolock); |
| 101 | |
| 102 | ret = mfd_add_devices(max8998->dev, -1, |
| 103 | max8998_devs, ARRAY_SIZE(max8998_devs), |
| 104 | NULL, 0); |
| 105 | if (ret < 0) |
| 106 | goto err; |
| 107 | |
| 108 | return ret; |
| 109 | |
| 110 | err: |
| 111 | mfd_remove_devices(max8998->dev); |
| 112 | kfree(max8998); |
| 113 | return ret; |
| 114 | } |
| 115 | |
| 116 | static int max8998_i2c_remove(struct i2c_client *i2c) |
| 117 | { |
| 118 | struct max8998_dev *max8998 = i2c_get_clientdata(i2c); |
| 119 | |
| 120 | mfd_remove_devices(max8998->dev); |
| 121 | kfree(max8998); |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | static const struct i2c_device_id max8998_i2c_id[] = { |
Kyungmin Park | f8539dd | 2010-08-23 13:46:49 +0900 | [diff] [blame] | 127 | { "max8998", 0 }, |
| 128 | { "lp3974", 0 }, |
| 129 | { } |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 130 | }; |
| 131 | MODULE_DEVICE_TABLE(i2c, max8998_i2c_id); |
| 132 | |
| 133 | static struct i2c_driver max8998_i2c_driver = { |
| 134 | .driver = { |
| 135 | .name = "max8998", |
| 136 | .owner = THIS_MODULE, |
| 137 | }, |
| 138 | .probe = max8998_i2c_probe, |
| 139 | .remove = max8998_i2c_remove, |
| 140 | .id_table = max8998_i2c_id, |
| 141 | }; |
| 142 | |
| 143 | static int __init max8998_i2c_init(void) |
| 144 | { |
| 145 | return i2c_add_driver(&max8998_i2c_driver); |
| 146 | } |
| 147 | /* init early so consumer devices can complete system boot */ |
| 148 | subsys_initcall(max8998_i2c_init); |
| 149 | |
| 150 | static void __exit max8998_i2c_exit(void) |
| 151 | { |
| 152 | i2c_del_driver(&max8998_i2c_driver); |
| 153 | } |
| 154 | module_exit(max8998_i2c_exit); |
| 155 | |
| 156 | MODULE_DESCRIPTION("MAXIM 8998 multi-function core driver"); |
| 157 | MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>"); |
| 158 | MODULE_LICENSE("GPL"); |