Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * drivers/hwmon/lis3lv02d_i2c.c |
| 3 | * |
| 4 | * Implements I2C interface for lis3lv02d (STMicroelectronics) accelerometer. |
| 5 | * Driver is based on corresponding SPI driver written by Daniel Mack |
| 6 | * (lis3lv02d_spi.c (C) 2009 Daniel Mack <daniel@caiaq.de> ). |
| 7 | * |
| 8 | * Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
| 9 | * |
| 10 | * Contact: Samu Onkalo <samu.p.onkalo@nokia.com> |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License |
| 14 | * version 2 as published by the Free Software Foundation. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, but |
| 17 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 19 | * General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 24 | * 02110-1301 USA |
| 25 | */ |
| 26 | |
| 27 | #include <linux/module.h> |
| 28 | #include <linux/kernel.h> |
| 29 | #include <linux/init.h> |
| 30 | #include <linux/err.h> |
| 31 | #include <linux/i2c.h> |
Samu Onkalo | 2a34699 | 2010-10-22 07:57:23 -0400 | [diff] [blame^] | 32 | #include <linux/pm_runtime.h> |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 33 | #include "lis3lv02d.h" |
| 34 | |
| 35 | #define DRV_NAME "lis3lv02d_i2c" |
| 36 | |
| 37 | static inline s32 lis3_i2c_write(struct lis3lv02d *lis3, int reg, u8 value) |
| 38 | { |
| 39 | struct i2c_client *c = lis3->bus_priv; |
| 40 | return i2c_smbus_write_byte_data(c, reg, value); |
| 41 | } |
| 42 | |
| 43 | static inline s32 lis3_i2c_read(struct lis3lv02d *lis3, int reg, u8 *v) |
| 44 | { |
| 45 | struct i2c_client *c = lis3->bus_priv; |
| 46 | *v = i2c_smbus_read_byte_data(c, reg); |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | static int lis3_i2c_init(struct lis3lv02d *lis3) |
| 51 | { |
| 52 | u8 reg; |
| 53 | int ret; |
| 54 | |
| 55 | /* power up the device */ |
| 56 | ret = lis3->read(lis3, CTRL_REG1, ®); |
| 57 | if (ret < 0) |
| 58 | return ret; |
| 59 | |
| 60 | reg |= CTRL1_PD0; |
| 61 | return lis3->write(lis3, CTRL_REG1, reg); |
| 62 | } |
| 63 | |
| 64 | /* Default axis mapping but it can be overwritten by platform data */ |
Takashi Iwai | 2ee3214 | 2010-10-01 17:14:25 -0400 | [diff] [blame] | 65 | static union axis_conversion lis3lv02d_axis_map = |
| 66 | { .as_array = { LIS3_DEV_X, LIS3_DEV_Y, LIS3_DEV_Z } }; |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 67 | |
| 68 | static int __devinit lis3lv02d_i2c_probe(struct i2c_client *client, |
| 69 | const struct i2c_device_id *id) |
| 70 | { |
| 71 | int ret = 0; |
| 72 | struct lis3lv02d_platform_data *pdata = client->dev.platform_data; |
| 73 | |
| 74 | if (pdata) { |
| 75 | if (pdata->axis_x) |
| 76 | lis3lv02d_axis_map.x = pdata->axis_x; |
| 77 | |
| 78 | if (pdata->axis_y) |
| 79 | lis3lv02d_axis_map.y = pdata->axis_y; |
| 80 | |
| 81 | if (pdata->axis_z) |
| 82 | lis3lv02d_axis_map.z = pdata->axis_z; |
| 83 | |
| 84 | if (pdata->setup_resources) |
| 85 | ret = pdata->setup_resources(); |
| 86 | |
| 87 | if (ret) |
| 88 | goto fail; |
| 89 | } |
| 90 | |
| 91 | lis3_dev.pdata = pdata; |
| 92 | lis3_dev.bus_priv = client; |
| 93 | lis3_dev.init = lis3_i2c_init; |
| 94 | lis3_dev.read = lis3_i2c_read; |
| 95 | lis3_dev.write = lis3_i2c_write; |
| 96 | lis3_dev.irq = client->irq; |
| 97 | lis3_dev.ac = lis3lv02d_axis_map; |
Samu Onkalo | 2a34699 | 2010-10-22 07:57:23 -0400 | [diff] [blame^] | 98 | lis3_dev.pm_dev = &client->dev; |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 99 | |
| 100 | i2c_set_clientdata(client, &lis3_dev); |
| 101 | ret = lis3lv02d_init_device(&lis3_dev); |
| 102 | fail: |
| 103 | return ret; |
| 104 | } |
| 105 | |
| 106 | static int __devexit lis3lv02d_i2c_remove(struct i2c_client *client) |
| 107 | { |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 108 | struct lis3lv02d_platform_data *pdata = client->dev.platform_data; |
| 109 | |
| 110 | if (pdata && pdata->release_resources) |
| 111 | pdata->release_resources(); |
| 112 | |
| 113 | lis3lv02d_joystick_disable(); |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 114 | |
| 115 | return lis3lv02d_remove_fs(&lis3_dev); |
| 116 | } |
| 117 | |
| 118 | #ifdef CONFIG_PM |
Samu Onkalo | 2a34699 | 2010-10-22 07:57:23 -0400 | [diff] [blame^] | 119 | static int lis3lv02d_i2c_suspend(struct device *dev) |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 120 | { |
Samu Onkalo | 2a34699 | 2010-10-22 07:57:23 -0400 | [diff] [blame^] | 121 | struct i2c_client *client = container_of(dev, struct i2c_client, dev); |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 122 | struct lis3lv02d *lis3 = i2c_get_clientdata(client); |
| 123 | |
Kuninori Morimoto | 5facb09 | 2010-09-17 17:24:10 +0200 | [diff] [blame] | 124 | if (!lis3->pdata || !lis3->pdata->wakeup_flags) |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 125 | lis3lv02d_poweroff(lis3); |
| 126 | return 0; |
| 127 | } |
| 128 | |
Samu Onkalo | 2a34699 | 2010-10-22 07:57:23 -0400 | [diff] [blame^] | 129 | static int lis3lv02d_i2c_resume(struct device *dev) |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 130 | { |
Samu Onkalo | 2a34699 | 2010-10-22 07:57:23 -0400 | [diff] [blame^] | 131 | struct i2c_client *client = container_of(dev, struct i2c_client, dev); |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 132 | struct lis3lv02d *lis3 = i2c_get_clientdata(client); |
| 133 | |
Samu Onkalo | 2a34699 | 2010-10-22 07:57:23 -0400 | [diff] [blame^] | 134 | /* |
| 135 | * pm_runtime documentation says that devices should always |
| 136 | * be powered on at resume. Pm_runtime turns them off after system |
| 137 | * wide resume is complete. |
| 138 | */ |
| 139 | if (!lis3->pdata || !lis3->pdata->wakeup_flags || |
| 140 | pm_runtime_suspended(dev)) |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 141 | lis3lv02d_poweron(lis3); |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 142 | |
Samu Onkalo | 2a34699 | 2010-10-22 07:57:23 -0400 | [diff] [blame^] | 143 | return 0; |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 144 | } |
| 145 | #else |
| 146 | #define lis3lv02d_i2c_suspend NULL |
| 147 | #define lis3lv02d_i2c_resume NULL |
| 148 | #define lis3lv02d_i2c_shutdown NULL |
| 149 | #endif |
| 150 | |
Samu Onkalo | 2a34699 | 2010-10-22 07:57:23 -0400 | [diff] [blame^] | 151 | static int lis3_i2c_runtime_suspend(struct device *dev) |
| 152 | { |
| 153 | struct i2c_client *client = container_of(dev, struct i2c_client, dev); |
| 154 | struct lis3lv02d *lis3 = i2c_get_clientdata(client); |
| 155 | |
| 156 | lis3lv02d_poweroff(lis3); |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | static int lis3_i2c_runtime_resume(struct device *dev) |
| 161 | { |
| 162 | struct i2c_client *client = container_of(dev, struct i2c_client, dev); |
| 163 | struct lis3lv02d *lis3 = i2c_get_clientdata(client); |
| 164 | |
| 165 | lis3lv02d_poweron(lis3); |
| 166 | return 0; |
| 167 | } |
| 168 | |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 169 | static const struct i2c_device_id lis3lv02d_id[] = { |
| 170 | {"lis3lv02d", 0 }, |
| 171 | {} |
| 172 | }; |
| 173 | |
| 174 | MODULE_DEVICE_TABLE(i2c, lis3lv02d_id); |
| 175 | |
Samu Onkalo | 2a34699 | 2010-10-22 07:57:23 -0400 | [diff] [blame^] | 176 | static const struct dev_pm_ops lis3_pm_ops = { |
| 177 | SET_SYSTEM_SLEEP_PM_OPS(lis3lv02d_i2c_suspend, |
| 178 | lis3lv02d_i2c_resume) |
| 179 | SET_RUNTIME_PM_OPS(lis3_i2c_runtime_suspend, |
| 180 | lis3_i2c_runtime_resume, |
| 181 | NULL) |
| 182 | }; |
| 183 | |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 184 | static struct i2c_driver lis3lv02d_i2c_driver = { |
| 185 | .driver = { |
| 186 | .name = DRV_NAME, |
| 187 | .owner = THIS_MODULE, |
Samu Onkalo | 2a34699 | 2010-10-22 07:57:23 -0400 | [diff] [blame^] | 188 | .pm = &lis3_pm_ops, |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 189 | }, |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 190 | .probe = lis3lv02d_i2c_probe, |
| 191 | .remove = __devexit_p(lis3lv02d_i2c_remove), |
| 192 | .id_table = lis3lv02d_id, |
| 193 | }; |
| 194 | |
| 195 | static int __init lis3lv02d_init(void) |
| 196 | { |
| 197 | return i2c_add_driver(&lis3lv02d_i2c_driver); |
| 198 | } |
| 199 | |
| 200 | static void __exit lis3lv02d_exit(void) |
| 201 | { |
| 202 | i2c_del_driver(&lis3lv02d_i2c_driver); |
| 203 | } |
| 204 | |
| 205 | MODULE_AUTHOR("Nokia Corporation"); |
| 206 | MODULE_DESCRIPTION("lis3lv02d I2C interface"); |
| 207 | MODULE_LICENSE("GPL"); |
| 208 | |
| 209 | module_init(lis3lv02d_init); |
| 210 | module_exit(lis3lv02d_exit); |