Haojian Zhuang | d50f8f3 | 2010-01-08 12:29:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * I2C driver for Maxim MAX8925 |
| 3 | * |
| 4 | * Copyright (C) 2009 Marvell International Ltd. |
| 5 | * Haojian Zhuang <haojian.zhuang@marvell.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/platform_device.h> |
| 14 | #include <linux/i2c.h> |
| 15 | #include <linux/mfd/max8925.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 16 | #include <linux/slab.h> |
Haojian Zhuang | d50f8f3 | 2010-01-08 12:29:23 +0100 | [diff] [blame] | 17 | |
Haojian Zhuang | b13c0df | 2010-01-25 06:26:34 -0500 | [diff] [blame] | 18 | #define RTC_I2C_ADDR 0x68 |
| 19 | #define ADC_I2C_ADDR 0x47 |
| 20 | |
Haojian Zhuang | d50f8f3 | 2010-01-08 12:29:23 +0100 | [diff] [blame] | 21 | static inline int max8925_read_device(struct i2c_client *i2c, |
| 22 | int reg, int bytes, void *dest) |
| 23 | { |
Haojian Zhuang | d50f8f3 | 2010-01-08 12:29:23 +0100 | [diff] [blame] | 24 | int ret; |
| 25 | |
Haojian Zhuang | b13c0df | 2010-01-25 06:26:34 -0500 | [diff] [blame] | 26 | if (bytes > 1) |
| 27 | ret = i2c_smbus_read_i2c_block_data(i2c, reg, bytes, dest); |
| 28 | else { |
| 29 | ret = i2c_smbus_read_byte_data(i2c, reg); |
| 30 | if (ret < 0) |
| 31 | return ret; |
| 32 | *(unsigned char *)dest = (unsigned char)ret; |
| 33 | } |
| 34 | return ret; |
Haojian Zhuang | d50f8f3 | 2010-01-08 12:29:23 +0100 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | static inline int max8925_write_device(struct i2c_client *i2c, |
| 38 | int reg, int bytes, void *src) |
| 39 | { |
| 40 | unsigned char buf[bytes + 1]; |
| 41 | int ret; |
| 42 | |
| 43 | buf[0] = (unsigned char)reg; |
| 44 | memcpy(&buf[1], src, bytes); |
| 45 | |
| 46 | ret = i2c_master_send(i2c, buf, bytes + 1); |
| 47 | if (ret < 0) |
| 48 | return ret; |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | int max8925_reg_read(struct i2c_client *i2c, int reg) |
| 53 | { |
| 54 | struct max8925_chip *chip = i2c_get_clientdata(i2c); |
Haojian Zhuang | b13c0df | 2010-01-25 06:26:34 -0500 | [diff] [blame] | 55 | unsigned char data = 0; |
Haojian Zhuang | d50f8f3 | 2010-01-08 12:29:23 +0100 | [diff] [blame] | 56 | int ret; |
| 57 | |
| 58 | mutex_lock(&chip->io_lock); |
| 59 | ret = max8925_read_device(i2c, reg, 1, &data); |
| 60 | mutex_unlock(&chip->io_lock); |
| 61 | |
| 62 | if (ret < 0) |
| 63 | return ret; |
| 64 | else |
| 65 | return (int)data; |
| 66 | } |
| 67 | EXPORT_SYMBOL(max8925_reg_read); |
| 68 | |
| 69 | int max8925_reg_write(struct i2c_client *i2c, int reg, |
| 70 | unsigned char data) |
| 71 | { |
| 72 | struct max8925_chip *chip = i2c_get_clientdata(i2c); |
| 73 | int ret; |
| 74 | |
| 75 | mutex_lock(&chip->io_lock); |
| 76 | ret = max8925_write_device(i2c, reg, 1, &data); |
| 77 | mutex_unlock(&chip->io_lock); |
| 78 | |
| 79 | return ret; |
| 80 | } |
| 81 | EXPORT_SYMBOL(max8925_reg_write); |
| 82 | |
| 83 | int max8925_bulk_read(struct i2c_client *i2c, int reg, |
| 84 | int count, unsigned char *buf) |
| 85 | { |
| 86 | struct max8925_chip *chip = i2c_get_clientdata(i2c); |
| 87 | int ret; |
| 88 | |
| 89 | mutex_lock(&chip->io_lock); |
| 90 | ret = max8925_read_device(i2c, reg, count, buf); |
| 91 | mutex_unlock(&chip->io_lock); |
| 92 | |
| 93 | return ret; |
| 94 | } |
| 95 | EXPORT_SYMBOL(max8925_bulk_read); |
| 96 | |
| 97 | int max8925_bulk_write(struct i2c_client *i2c, int reg, |
| 98 | int count, unsigned char *buf) |
| 99 | { |
| 100 | struct max8925_chip *chip = i2c_get_clientdata(i2c); |
| 101 | int ret; |
| 102 | |
| 103 | mutex_lock(&chip->io_lock); |
| 104 | ret = max8925_write_device(i2c, reg, count, buf); |
| 105 | mutex_unlock(&chip->io_lock); |
| 106 | |
| 107 | return ret; |
| 108 | } |
| 109 | EXPORT_SYMBOL(max8925_bulk_write); |
| 110 | |
| 111 | int max8925_set_bits(struct i2c_client *i2c, int reg, |
| 112 | unsigned char mask, unsigned char data) |
| 113 | { |
| 114 | struct max8925_chip *chip = i2c_get_clientdata(i2c); |
| 115 | unsigned char value; |
| 116 | int ret; |
| 117 | |
| 118 | mutex_lock(&chip->io_lock); |
| 119 | ret = max8925_read_device(i2c, reg, 1, &value); |
| 120 | if (ret < 0) |
| 121 | goto out; |
| 122 | value &= ~mask; |
| 123 | value |= data; |
| 124 | ret = max8925_write_device(i2c, reg, 1, &value); |
| 125 | out: |
| 126 | mutex_unlock(&chip->io_lock); |
| 127 | return ret; |
| 128 | } |
| 129 | EXPORT_SYMBOL(max8925_set_bits); |
| 130 | |
| 131 | |
| 132 | static const struct i2c_device_id max8925_id_table[] = { |
| 133 | { "max8925", 0 }, |
Haojian Zhuang | b13c0df | 2010-01-25 06:26:34 -0500 | [diff] [blame] | 134 | { }, |
Haojian Zhuang | d50f8f3 | 2010-01-08 12:29:23 +0100 | [diff] [blame] | 135 | }; |
| 136 | MODULE_DEVICE_TABLE(i2c, max8925_id_table); |
| 137 | |
Qing Xu | 4e405ae | 2013-02-04 23:40:42 +0800 | [diff] [blame] | 138 | static int max8925_dt_init(struct device_node *np, struct device *dev, |
| 139 | struct max8925_platform_data *pdata) |
| 140 | { |
| 141 | int ret; |
| 142 | |
| 143 | ret = of_property_read_u32(np, "maxim,tsc-irq", &pdata->tsc_irq); |
| 144 | if (ret) { |
| 145 | dev_err(dev, "Not found maxim,tsc-irq property\n"); |
| 146 | return -EINVAL; |
| 147 | } |
| 148 | return 0; |
| 149 | } |
| 150 | |
Bill Pemberton | f791be4 | 2012-11-19 13:23:04 -0500 | [diff] [blame] | 151 | static int max8925_probe(struct i2c_client *client, |
Haojian Zhuang | d50f8f3 | 2010-01-08 12:29:23 +0100 | [diff] [blame] | 152 | const struct i2c_device_id *id) |
| 153 | { |
Jingoo Han | 334a41c | 2013-07-30 17:10:05 +0900 | [diff] [blame] | 154 | struct max8925_platform_data *pdata = dev_get_platdata(&client->dev); |
Haojian Zhuang | b13c0df | 2010-01-25 06:26:34 -0500 | [diff] [blame] | 155 | static struct max8925_chip *chip; |
Qing Xu | 4e405ae | 2013-02-04 23:40:42 +0800 | [diff] [blame] | 156 | struct device_node *node = client->dev.of_node; |
Haojian Zhuang | d50f8f3 | 2010-01-08 12:29:23 +0100 | [diff] [blame] | 157 | |
Qing Xu | 4e405ae | 2013-02-04 23:40:42 +0800 | [diff] [blame] | 158 | if (node && !pdata) { |
| 159 | /* parse DT to get platform data */ |
| 160 | pdata = devm_kzalloc(&client->dev, |
| 161 | sizeof(struct max8925_platform_data), |
| 162 | GFP_KERNEL); |
| 163 | if (!pdata) |
| 164 | return -ENOMEM; |
| 165 | |
| 166 | if (max8925_dt_init(node, &client->dev, pdata)) |
| 167 | return -EINVAL; |
| 168 | } else if (!pdata) { |
Haojian Zhuang | d50f8f3 | 2010-01-08 12:29:23 +0100 | [diff] [blame] | 169 | pr_info("%s: platform data is missing\n", __func__); |
| 170 | return -EINVAL; |
| 171 | } |
Haojian Zhuang | d50f8f3 | 2010-01-08 12:29:23 +0100 | [diff] [blame] | 172 | |
Lee Jones | 02c7d84 | 2013-05-23 16:25:17 +0100 | [diff] [blame] | 173 | chip = devm_kzalloc(&client->dev, |
| 174 | sizeof(struct max8925_chip), GFP_KERNEL); |
Haojian Zhuang | d50f8f3 | 2010-01-08 12:29:23 +0100 | [diff] [blame] | 175 | if (chip == NULL) |
| 176 | return -ENOMEM; |
| 177 | chip->i2c = client; |
Haojian Zhuang | d50f8f3 | 2010-01-08 12:29:23 +0100 | [diff] [blame] | 178 | chip->dev = &client->dev; |
Haojian Zhuang | b13c0df | 2010-01-25 06:26:34 -0500 | [diff] [blame] | 179 | i2c_set_clientdata(client, chip); |
Haojian Zhuang | d50f8f3 | 2010-01-08 12:29:23 +0100 | [diff] [blame] | 180 | dev_set_drvdata(chip->dev, chip); |
Haojian Zhuang | b13c0df | 2010-01-25 06:26:34 -0500 | [diff] [blame] | 181 | mutex_init(&chip->io_lock); |
| 182 | |
| 183 | chip->rtc = i2c_new_dummy(chip->i2c->adapter, RTC_I2C_ADDR); |
Krzysztof Kozlowski | 96cf3de | 2014-02-11 11:03:33 +0100 | [diff] [blame] | 184 | if (!chip->rtc) { |
| 185 | dev_err(chip->dev, "Failed to allocate I2C device for RTC\n"); |
| 186 | return -ENODEV; |
| 187 | } |
Haojian Zhuang | b13c0df | 2010-01-25 06:26:34 -0500 | [diff] [blame] | 188 | i2c_set_clientdata(chip->rtc, chip); |
| 189 | |
| 190 | chip->adc = i2c_new_dummy(chip->i2c->adapter, ADC_I2C_ADDR); |
Krzysztof Kozlowski | 96cf3de | 2014-02-11 11:03:33 +0100 | [diff] [blame] | 191 | if (!chip->adc) { |
| 192 | dev_err(chip->dev, "Failed to allocate I2C device for ADC\n"); |
| 193 | i2c_unregister_device(chip->rtc); |
| 194 | return -ENODEV; |
| 195 | } |
Haojian Zhuang | b13c0df | 2010-01-25 06:26:34 -0500 | [diff] [blame] | 196 | i2c_set_clientdata(chip->adc, chip); |
| 197 | |
Kevin Liu | ba74e80 | 2012-01-04 15:14:24 +0800 | [diff] [blame] | 198 | device_init_wakeup(&client->dev, 1); |
| 199 | |
Haojian Zhuang | d50f8f3 | 2010-01-08 12:29:23 +0100 | [diff] [blame] | 200 | max8925_device_init(chip, pdata); |
| 201 | |
| 202 | return 0; |
| 203 | } |
| 204 | |
Bill Pemberton | 4740f73 | 2012-11-19 13:26:01 -0500 | [diff] [blame] | 205 | static int max8925_remove(struct i2c_client *client) |
Haojian Zhuang | d50f8f3 | 2010-01-08 12:29:23 +0100 | [diff] [blame] | 206 | { |
| 207 | struct max8925_chip *chip = i2c_get_clientdata(client); |
| 208 | |
| 209 | max8925_device_exit(chip); |
Haojian Zhuang | b13c0df | 2010-01-25 06:26:34 -0500 | [diff] [blame] | 210 | i2c_unregister_device(chip->adc); |
| 211 | i2c_unregister_device(chip->rtc); |
Haojian Zhuang | d50f8f3 | 2010-01-08 12:29:23 +0100 | [diff] [blame] | 212 | return 0; |
| 213 | } |
| 214 | |
Kevin Liu | ba74e80 | 2012-01-04 15:14:24 +0800 | [diff] [blame] | 215 | #ifdef CONFIG_PM_SLEEP |
| 216 | static int max8925_suspend(struct device *dev) |
| 217 | { |
| 218 | struct i2c_client *client = container_of(dev, struct i2c_client, dev); |
| 219 | struct max8925_chip *chip = i2c_get_clientdata(client); |
| 220 | |
| 221 | if (device_may_wakeup(dev) && chip->wakeup_flag) |
| 222 | enable_irq_wake(chip->core_irq); |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | static int max8925_resume(struct device *dev) |
| 227 | { |
| 228 | struct i2c_client *client = container_of(dev, struct i2c_client, dev); |
| 229 | struct max8925_chip *chip = i2c_get_clientdata(client); |
| 230 | |
| 231 | if (device_may_wakeup(dev) && chip->wakeup_flag) |
| 232 | disable_irq_wake(chip->core_irq); |
| 233 | return 0; |
| 234 | } |
| 235 | #endif |
| 236 | |
| 237 | static SIMPLE_DEV_PM_OPS(max8925_pm_ops, max8925_suspend, max8925_resume); |
| 238 | |
Qing Xu | 4e405ae | 2013-02-04 23:40:42 +0800 | [diff] [blame] | 239 | static const struct of_device_id max8925_dt_ids[] = { |
| 240 | { .compatible = "maxim,max8925", }, |
| 241 | {}, |
| 242 | }; |
| 243 | MODULE_DEVICE_TABLE(of, max8925_dt_ids); |
| 244 | |
Haojian Zhuang | d50f8f3 | 2010-01-08 12:29:23 +0100 | [diff] [blame] | 245 | static struct i2c_driver max8925_driver = { |
| 246 | .driver = { |
| 247 | .name = "max8925", |
| 248 | .owner = THIS_MODULE, |
Kevin Liu | ba74e80 | 2012-01-04 15:14:24 +0800 | [diff] [blame] | 249 | .pm = &max8925_pm_ops, |
Sachin Kamat | 52c2c6e | 2013-10-15 09:18:46 +0530 | [diff] [blame] | 250 | .of_match_table = max8925_dt_ids, |
Haojian Zhuang | d50f8f3 | 2010-01-08 12:29:23 +0100 | [diff] [blame] | 251 | }, |
| 252 | .probe = max8925_probe, |
Bill Pemberton | 8444921 | 2012-11-19 13:20:24 -0500 | [diff] [blame] | 253 | .remove = max8925_remove, |
Haojian Zhuang | d50f8f3 | 2010-01-08 12:29:23 +0100 | [diff] [blame] | 254 | .id_table = max8925_id_table, |
| 255 | }; |
| 256 | |
| 257 | static int __init max8925_i2c_init(void) |
| 258 | { |
| 259 | int ret; |
Lee Jones | 4ed8f71 | 2014-07-21 12:18:21 +0100 | [diff] [blame] | 260 | |
Haojian Zhuang | d50f8f3 | 2010-01-08 12:29:23 +0100 | [diff] [blame] | 261 | ret = i2c_add_driver(&max8925_driver); |
| 262 | if (ret != 0) |
| 263 | pr_err("Failed to register MAX8925 I2C driver: %d\n", ret); |
Lee Jones | 4ed8f71 | 2014-07-21 12:18:21 +0100 | [diff] [blame] | 264 | |
Haojian Zhuang | d50f8f3 | 2010-01-08 12:29:23 +0100 | [diff] [blame] | 265 | return ret; |
| 266 | } |
| 267 | subsys_initcall(max8925_i2c_init); |
| 268 | |
| 269 | static void __exit max8925_i2c_exit(void) |
| 270 | { |
| 271 | i2c_del_driver(&max8925_driver); |
| 272 | } |
| 273 | module_exit(max8925_i2c_exit); |
| 274 | |
| 275 | MODULE_DESCRIPTION("I2C Driver for Maxim 8925"); |
| 276 | MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>"); |
| 277 | MODULE_LICENSE("GPL"); |