Mark Brown | c661a0b | 2008-10-10 15:58:11 +0100 | [diff] [blame] | 1 | /* |
| 2 | * wm8350-i2c.c -- Generic I2C driver for Wolfson WM8350 PMIC |
| 3 | * |
Mark Brown | c661a0b | 2008-10-10 15:58:11 +0100 | [diff] [blame] | 4 | * Copyright 2007, 2008 Wolfson Microelectronics PLC. |
| 5 | * |
| 6 | * Author: Liam Girdwood |
| 7 | * linux@wolfsonmicro.com |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify it |
| 10 | * under the terms of the GNU General Public License as published by the |
| 11 | * Free Software Foundation; either version 2 of the License, or (at your |
| 12 | * option) any later version. |
| 13 | * |
| 14 | */ |
| 15 | |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/moduleparam.h> |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/i2c.h> |
| 20 | #include <linux/platform_device.h> |
| 21 | #include <linux/mfd/wm8350/core.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
Mark Brown | c661a0b | 2008-10-10 15:58:11 +0100 | [diff] [blame] | 23 | |
| 24 | static int wm8350_i2c_read_device(struct wm8350 *wm8350, char reg, |
| 25 | int bytes, void *dest) |
| 26 | { |
| 27 | int ret; |
| 28 | |
| 29 | ret = i2c_master_send(wm8350->i2c_client, ®, 1); |
| 30 | if (ret < 0) |
| 31 | return ret; |
Mark Brown | 898d805 | 2008-11-12 17:34:02 +0100 | [diff] [blame] | 32 | ret = i2c_master_recv(wm8350->i2c_client, dest, bytes); |
| 33 | if (ret < 0) |
| 34 | return ret; |
| 35 | if (ret != bytes) |
| 36 | return -EIO; |
| 37 | return 0; |
Mark Brown | c661a0b | 2008-10-10 15:58:11 +0100 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | static int wm8350_i2c_write_device(struct wm8350 *wm8350, char reg, |
| 41 | int bytes, void *src) |
| 42 | { |
| 43 | /* we add 1 byte for device register */ |
| 44 | u8 msg[(WM8350_MAX_REGISTER << 1) + 1]; |
Mark Brown | 898d805 | 2008-11-12 17:34:02 +0100 | [diff] [blame] | 45 | int ret; |
Mark Brown | c661a0b | 2008-10-10 15:58:11 +0100 | [diff] [blame] | 46 | |
| 47 | if (bytes > ((WM8350_MAX_REGISTER << 1) + 1)) |
| 48 | return -EINVAL; |
| 49 | |
| 50 | msg[0] = reg; |
| 51 | memcpy(&msg[1], src, bytes); |
Mark Brown | 898d805 | 2008-11-12 17:34:02 +0100 | [diff] [blame] | 52 | ret = i2c_master_send(wm8350->i2c_client, msg, bytes + 1); |
| 53 | if (ret < 0) |
| 54 | return ret; |
| 55 | if (ret != bytes + 1) |
| 56 | return -EIO; |
| 57 | return 0; |
Mark Brown | c661a0b | 2008-10-10 15:58:11 +0100 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | static int wm8350_i2c_probe(struct i2c_client *i2c, |
| 61 | const struct i2c_device_id *id) |
| 62 | { |
| 63 | struct wm8350 *wm8350; |
| 64 | int ret = 0; |
| 65 | |
| 66 | wm8350 = kzalloc(sizeof(struct wm8350), GFP_KERNEL); |
Rabin Vincent | e47a3bb | 2010-04-21 21:06:05 +0530 | [diff] [blame] | 67 | if (wm8350 == NULL) |
Mark Brown | c661a0b | 2008-10-10 15:58:11 +0100 | [diff] [blame] | 68 | return -ENOMEM; |
Mark Brown | c661a0b | 2008-10-10 15:58:11 +0100 | [diff] [blame] | 69 | |
| 70 | i2c_set_clientdata(i2c, wm8350); |
| 71 | wm8350->dev = &i2c->dev; |
| 72 | wm8350->i2c_client = i2c; |
| 73 | wm8350->read_dev = wm8350_i2c_read_device; |
| 74 | wm8350->write_dev = wm8350_i2c_write_device; |
| 75 | |
Mark Brown | ebccec0 | 2008-10-10 15:58:14 +0100 | [diff] [blame] | 76 | ret = wm8350_device_init(wm8350, i2c->irq, i2c->dev.platform_data); |
Mark Brown | c661a0b | 2008-10-10 15:58:11 +0100 | [diff] [blame] | 77 | if (ret < 0) |
| 78 | goto err; |
| 79 | |
| 80 | return ret; |
| 81 | |
| 82 | err: |
Wolfram Sang | f322d5f | 2010-03-20 15:12:54 +0100 | [diff] [blame] | 83 | i2c_set_clientdata(i2c, NULL); |
Mark Brown | c661a0b | 2008-10-10 15:58:11 +0100 | [diff] [blame] | 84 | kfree(wm8350); |
| 85 | return ret; |
| 86 | } |
| 87 | |
| 88 | static int wm8350_i2c_remove(struct i2c_client *i2c) |
| 89 | { |
| 90 | struct wm8350 *wm8350 = i2c_get_clientdata(i2c); |
| 91 | |
| 92 | wm8350_device_exit(wm8350); |
Wolfram Sang | f322d5f | 2010-03-20 15:12:54 +0100 | [diff] [blame] | 93 | i2c_set_clientdata(i2c, NULL); |
Mark Brown | c661a0b | 2008-10-10 15:58:11 +0100 | [diff] [blame] | 94 | kfree(wm8350); |
| 95 | |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | static const struct i2c_device_id wm8350_i2c_id[] = { |
| 100 | { "wm8350", 0 }, |
Mark Brown | ca23f8c | 2008-12-18 23:12:28 +0100 | [diff] [blame] | 101 | { "wm8351", 0 }, |
Mark Brown | 9692063 | 2008-12-18 23:09:50 +0100 | [diff] [blame] | 102 | { "wm8352", 0 }, |
Mark Brown | c661a0b | 2008-10-10 15:58:11 +0100 | [diff] [blame] | 103 | { } |
| 104 | }; |
| 105 | MODULE_DEVICE_TABLE(i2c, wm8350_i2c_id); |
| 106 | |
| 107 | |
| 108 | static struct i2c_driver wm8350_i2c_driver = { |
| 109 | .driver = { |
| 110 | .name = "wm8350", |
| 111 | .owner = THIS_MODULE, |
| 112 | }, |
| 113 | .probe = wm8350_i2c_probe, |
| 114 | .remove = wm8350_i2c_remove, |
| 115 | .id_table = wm8350_i2c_id, |
| 116 | }; |
| 117 | |
| 118 | static int __init wm8350_i2c_init(void) |
| 119 | { |
| 120 | return i2c_add_driver(&wm8350_i2c_driver); |
| 121 | } |
| 122 | /* init early so consumer devices can complete system boot */ |
| 123 | subsys_initcall(wm8350_i2c_init); |
| 124 | |
| 125 | static void __exit wm8350_i2c_exit(void) |
| 126 | { |
| 127 | i2c_del_driver(&wm8350_i2c_driver); |
| 128 | } |
| 129 | module_exit(wm8350_i2c_exit); |
| 130 | |
| 131 | MODULE_DESCRIPTION("I2C support for the WM8350 AudioPlus PMIC"); |
| 132 | MODULE_LICENSE("GPL"); |