Mark Brown | e5b4868 | 2010-10-19 23:57:56 +0200 | [diff] [blame] | 1 | /* |
| 2 | * wm831x-i2c.c -- I2C access for Wolfson WM831x PMICs |
| 3 | * |
| 4 | * Copyright 2009,2010 Wolfson Microelectronics PLC. |
| 5 | * |
| 6 | * Author: Mark Brown <broonie@opensource.wolfsonmicro.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License as published by the |
| 10 | * Free Software Foundation; either version 2 of the License, or (at your |
| 11 | * option) any later version. |
| 12 | * |
| 13 | */ |
| 14 | |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/i2c.h> |
| 18 | #include <linux/delay.h> |
| 19 | #include <linux/mfd/core.h> |
| 20 | #include <linux/slab.h> |
| 21 | |
| 22 | #include <linux/mfd/wm831x/core.h> |
| 23 | #include <linux/mfd/wm831x/pdata.h> |
| 24 | |
| 25 | static int wm831x_i2c_read_device(struct wm831x *wm831x, unsigned short reg, |
| 26 | int bytes, void *dest) |
| 27 | { |
| 28 | struct i2c_client *i2c = wm831x->control_data; |
| 29 | int ret; |
| 30 | u16 r = cpu_to_be16(reg); |
| 31 | |
| 32 | ret = i2c_master_send(i2c, (unsigned char *)&r, 2); |
| 33 | if (ret < 0) |
| 34 | return ret; |
| 35 | if (ret != 2) |
| 36 | return -EIO; |
| 37 | |
| 38 | ret = i2c_master_recv(i2c, dest, bytes); |
| 39 | if (ret < 0) |
| 40 | return ret; |
| 41 | if (ret != bytes) |
| 42 | return -EIO; |
| 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | /* Currently we allocate the write buffer on the stack; this is OK for |
| 47 | * small writes - if we need to do large writes this will need to be |
| 48 | * revised. |
| 49 | */ |
| 50 | static int wm831x_i2c_write_device(struct wm831x *wm831x, unsigned short reg, |
| 51 | int bytes, void *src) |
| 52 | { |
| 53 | struct i2c_client *i2c = wm831x->control_data; |
| 54 | unsigned char msg[bytes + 2]; |
| 55 | int ret; |
| 56 | |
| 57 | reg = cpu_to_be16(reg); |
| 58 | memcpy(&msg[0], ®, 2); |
| 59 | memcpy(&msg[2], src, bytes); |
| 60 | |
| 61 | ret = i2c_master_send(i2c, msg, bytes + 2); |
| 62 | if (ret < 0) |
| 63 | return ret; |
| 64 | if (ret < bytes + 2) |
| 65 | return -EIO; |
| 66 | |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | static int wm831x_i2c_probe(struct i2c_client *i2c, |
| 71 | const struct i2c_device_id *id) |
| 72 | { |
| 73 | struct wm831x *wm831x; |
| 74 | |
| 75 | wm831x = kzalloc(sizeof(struct wm831x), GFP_KERNEL); |
| 76 | if (wm831x == NULL) |
| 77 | return -ENOMEM; |
| 78 | |
| 79 | i2c_set_clientdata(i2c, wm831x); |
| 80 | wm831x->dev = &i2c->dev; |
| 81 | wm831x->control_data = i2c; |
| 82 | wm831x->read_dev = wm831x_i2c_read_device; |
| 83 | wm831x->write_dev = wm831x_i2c_write_device; |
| 84 | |
| 85 | return wm831x_device_init(wm831x, id->driver_data, i2c->irq); |
| 86 | } |
| 87 | |
| 88 | static int wm831x_i2c_remove(struct i2c_client *i2c) |
| 89 | { |
| 90 | struct wm831x *wm831x = i2c_get_clientdata(i2c); |
| 91 | |
| 92 | wm831x_device_exit(wm831x); |
| 93 | |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | static int wm831x_i2c_suspend(struct i2c_client *i2c, pm_message_t mesg) |
| 98 | { |
| 99 | struct wm831x *wm831x = i2c_get_clientdata(i2c); |
| 100 | |
| 101 | return wm831x_device_suspend(wm831x); |
| 102 | } |
| 103 | |
| 104 | static const struct i2c_device_id wm831x_i2c_id[] = { |
| 105 | { "wm8310", WM8310 }, |
| 106 | { "wm8311", WM8311 }, |
| 107 | { "wm8312", WM8312 }, |
| 108 | { "wm8320", WM8320 }, |
| 109 | { "wm8321", WM8321 }, |
| 110 | { "wm8325", WM8325 }, |
| 111 | { } |
| 112 | }; |
| 113 | MODULE_DEVICE_TABLE(i2c, wm831x_i2c_id); |
| 114 | |
| 115 | |
| 116 | static struct i2c_driver wm831x_i2c_driver = { |
| 117 | .driver = { |
| 118 | .name = "wm831x", |
| 119 | .owner = THIS_MODULE, |
| 120 | }, |
| 121 | .probe = wm831x_i2c_probe, |
| 122 | .remove = wm831x_i2c_remove, |
| 123 | .suspend = wm831x_i2c_suspend, |
| 124 | .id_table = wm831x_i2c_id, |
| 125 | }; |
| 126 | |
| 127 | static int __init wm831x_i2c_init(void) |
| 128 | { |
| 129 | int ret; |
| 130 | |
| 131 | ret = i2c_add_driver(&wm831x_i2c_driver); |
| 132 | if (ret != 0) |
| 133 | pr_err("Failed to register wm831x I2C driver: %d\n", ret); |
| 134 | |
| 135 | return ret; |
| 136 | } |
| 137 | subsys_initcall(wm831x_i2c_init); |
| 138 | |
| 139 | static void __exit wm831x_i2c_exit(void) |
| 140 | { |
| 141 | i2c_del_driver(&wm831x_i2c_driver); |
| 142 | } |
| 143 | module_exit(wm831x_i2c_exit); |