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 | f9deb41 | 2010-10-22 07:57:24 -0400 | [diff] [blame^] | 33 | #include <linux/delay.h> |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 34 | #include "lis3lv02d.h" |
| 35 | |
| 36 | #define DRV_NAME "lis3lv02d_i2c" |
| 37 | |
Samu Onkalo | f9deb41 | 2010-10-22 07:57:24 -0400 | [diff] [blame^] | 38 | static const char reg_vdd[] = "Vdd"; |
| 39 | static const char reg_vdd_io[] = "Vdd_IO"; |
| 40 | |
| 41 | static int lis3_reg_ctrl(struct lis3lv02d *lis3, bool state) |
| 42 | { |
| 43 | int ret; |
| 44 | if (state == LIS3_REG_OFF) { |
| 45 | ret = regulator_bulk_disable(ARRAY_SIZE(lis3->regulators), |
| 46 | lis3->regulators); |
| 47 | } else { |
| 48 | ret = regulator_bulk_enable(ARRAY_SIZE(lis3->regulators), |
| 49 | lis3->regulators); |
| 50 | /* Chip needs time to wakeup. Not mentioned in datasheet */ |
| 51 | usleep_range(10000, 20000); |
| 52 | } |
| 53 | return ret; |
| 54 | } |
| 55 | |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 56 | static inline s32 lis3_i2c_write(struct lis3lv02d *lis3, int reg, u8 value) |
| 57 | { |
| 58 | struct i2c_client *c = lis3->bus_priv; |
| 59 | return i2c_smbus_write_byte_data(c, reg, value); |
| 60 | } |
| 61 | |
| 62 | static inline s32 lis3_i2c_read(struct lis3lv02d *lis3, int reg, u8 *v) |
| 63 | { |
| 64 | struct i2c_client *c = lis3->bus_priv; |
| 65 | *v = i2c_smbus_read_byte_data(c, reg); |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | static int lis3_i2c_init(struct lis3lv02d *lis3) |
| 70 | { |
| 71 | u8 reg; |
| 72 | int ret; |
| 73 | |
Samu Onkalo | f9deb41 | 2010-10-22 07:57:24 -0400 | [diff] [blame^] | 74 | if (lis3->reg_ctrl) |
| 75 | lis3_reg_ctrl(lis3, LIS3_REG_ON); |
| 76 | |
| 77 | lis3->read(lis3, WHO_AM_I, ®); |
| 78 | if (reg != lis3->whoami) |
| 79 | printk(KERN_ERR "lis3: power on failure\n"); |
| 80 | |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 81 | /* power up the device */ |
| 82 | ret = lis3->read(lis3, CTRL_REG1, ®); |
| 83 | if (ret < 0) |
| 84 | return ret; |
| 85 | |
| 86 | reg |= CTRL1_PD0; |
| 87 | return lis3->write(lis3, CTRL_REG1, reg); |
| 88 | } |
| 89 | |
| 90 | /* Default axis mapping but it can be overwritten by platform data */ |
Takashi Iwai | 2ee3214 | 2010-10-01 17:14:25 -0400 | [diff] [blame] | 91 | static union axis_conversion lis3lv02d_axis_map = |
| 92 | { .as_array = { LIS3_DEV_X, LIS3_DEV_Y, LIS3_DEV_Z } }; |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 93 | |
| 94 | static int __devinit lis3lv02d_i2c_probe(struct i2c_client *client, |
| 95 | const struct i2c_device_id *id) |
| 96 | { |
| 97 | int ret = 0; |
| 98 | struct lis3lv02d_platform_data *pdata = client->dev.platform_data; |
| 99 | |
| 100 | if (pdata) { |
Samu Onkalo | f9deb41 | 2010-10-22 07:57:24 -0400 | [diff] [blame^] | 101 | /* Regulator control is optional */ |
| 102 | if (pdata->driver_features & LIS3_USE_REGULATOR_CTRL) |
| 103 | lis3_dev.reg_ctrl = lis3_reg_ctrl; |
| 104 | |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 105 | if (pdata->axis_x) |
| 106 | lis3lv02d_axis_map.x = pdata->axis_x; |
| 107 | |
| 108 | if (pdata->axis_y) |
| 109 | lis3lv02d_axis_map.y = pdata->axis_y; |
| 110 | |
| 111 | if (pdata->axis_z) |
| 112 | lis3lv02d_axis_map.z = pdata->axis_z; |
| 113 | |
| 114 | if (pdata->setup_resources) |
| 115 | ret = pdata->setup_resources(); |
| 116 | |
| 117 | if (ret) |
| 118 | goto fail; |
| 119 | } |
| 120 | |
Samu Onkalo | f9deb41 | 2010-10-22 07:57:24 -0400 | [diff] [blame^] | 121 | if (lis3_dev.reg_ctrl) { |
| 122 | lis3_dev.regulators[0].supply = reg_vdd; |
| 123 | lis3_dev.regulators[1].supply = reg_vdd_io; |
| 124 | ret = regulator_bulk_get(&client->dev, |
| 125 | ARRAY_SIZE(lis3_dev.regulators), |
| 126 | lis3_dev.regulators); |
| 127 | if (ret < 0) |
| 128 | goto fail; |
| 129 | } |
| 130 | |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 131 | lis3_dev.pdata = pdata; |
| 132 | lis3_dev.bus_priv = client; |
| 133 | lis3_dev.init = lis3_i2c_init; |
| 134 | lis3_dev.read = lis3_i2c_read; |
| 135 | lis3_dev.write = lis3_i2c_write; |
| 136 | lis3_dev.irq = client->irq; |
| 137 | lis3_dev.ac = lis3lv02d_axis_map; |
Samu Onkalo | 2a34699 | 2010-10-22 07:57:23 -0400 | [diff] [blame] | 138 | lis3_dev.pm_dev = &client->dev; |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 139 | |
| 140 | i2c_set_clientdata(client, &lis3_dev); |
Samu Onkalo | f9deb41 | 2010-10-22 07:57:24 -0400 | [diff] [blame^] | 141 | |
| 142 | /* Provide power over the init call */ |
| 143 | if (lis3_dev.reg_ctrl) |
| 144 | lis3_reg_ctrl(&lis3_dev, LIS3_REG_ON); |
| 145 | |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 146 | ret = lis3lv02d_init_device(&lis3_dev); |
Samu Onkalo | f9deb41 | 2010-10-22 07:57:24 -0400 | [diff] [blame^] | 147 | |
| 148 | if (lis3_dev.reg_ctrl) |
| 149 | lis3_reg_ctrl(&lis3_dev, LIS3_REG_OFF); |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 150 | fail: |
| 151 | return ret; |
| 152 | } |
| 153 | |
| 154 | static int __devexit lis3lv02d_i2c_remove(struct i2c_client *client) |
| 155 | { |
Samu Onkalo | f9deb41 | 2010-10-22 07:57:24 -0400 | [diff] [blame^] | 156 | struct lis3lv02d *lis3 = i2c_get_clientdata(client); |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 157 | struct lis3lv02d_platform_data *pdata = client->dev.platform_data; |
| 158 | |
| 159 | if (pdata && pdata->release_resources) |
| 160 | pdata->release_resources(); |
| 161 | |
| 162 | lis3lv02d_joystick_disable(); |
Samu Onkalo | f9deb41 | 2010-10-22 07:57:24 -0400 | [diff] [blame^] | 163 | lis3lv02d_remove_fs(&lis3_dev); |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 164 | |
Samu Onkalo | f9deb41 | 2010-10-22 07:57:24 -0400 | [diff] [blame^] | 165 | if (lis3_dev.reg_ctrl) |
| 166 | regulator_bulk_free(ARRAY_SIZE(lis3->regulators), |
| 167 | lis3_dev.regulators); |
| 168 | return 0; |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | #ifdef CONFIG_PM |
Samu Onkalo | 2a34699 | 2010-10-22 07:57:23 -0400 | [diff] [blame] | 172 | static int lis3lv02d_i2c_suspend(struct device *dev) |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 173 | { |
Samu Onkalo | 2a34699 | 2010-10-22 07:57:23 -0400 | [diff] [blame] | 174 | struct i2c_client *client = container_of(dev, struct i2c_client, dev); |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 175 | struct lis3lv02d *lis3 = i2c_get_clientdata(client); |
| 176 | |
Kuninori Morimoto | 5facb09 | 2010-09-17 17:24:10 +0200 | [diff] [blame] | 177 | if (!lis3->pdata || !lis3->pdata->wakeup_flags) |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 178 | lis3lv02d_poweroff(lis3); |
| 179 | return 0; |
| 180 | } |
| 181 | |
Samu Onkalo | 2a34699 | 2010-10-22 07:57:23 -0400 | [diff] [blame] | 182 | static int lis3lv02d_i2c_resume(struct device *dev) |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 183 | { |
Samu Onkalo | 2a34699 | 2010-10-22 07:57:23 -0400 | [diff] [blame] | 184 | struct i2c_client *client = container_of(dev, struct i2c_client, dev); |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 185 | struct lis3lv02d *lis3 = i2c_get_clientdata(client); |
| 186 | |
Samu Onkalo | 2a34699 | 2010-10-22 07:57:23 -0400 | [diff] [blame] | 187 | /* |
| 188 | * pm_runtime documentation says that devices should always |
| 189 | * be powered on at resume. Pm_runtime turns them off after system |
| 190 | * wide resume is complete. |
| 191 | */ |
| 192 | if (!lis3->pdata || !lis3->pdata->wakeup_flags || |
| 193 | pm_runtime_suspended(dev)) |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 194 | lis3lv02d_poweron(lis3); |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 195 | |
Samu Onkalo | 2a34699 | 2010-10-22 07:57:23 -0400 | [diff] [blame] | 196 | return 0; |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 197 | } |
| 198 | #else |
| 199 | #define lis3lv02d_i2c_suspend NULL |
| 200 | #define lis3lv02d_i2c_resume NULL |
| 201 | #define lis3lv02d_i2c_shutdown NULL |
| 202 | #endif |
| 203 | |
Samu Onkalo | 2a34699 | 2010-10-22 07:57:23 -0400 | [diff] [blame] | 204 | static int lis3_i2c_runtime_suspend(struct device *dev) |
| 205 | { |
| 206 | struct i2c_client *client = container_of(dev, struct i2c_client, dev); |
| 207 | struct lis3lv02d *lis3 = i2c_get_clientdata(client); |
| 208 | |
| 209 | lis3lv02d_poweroff(lis3); |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | static int lis3_i2c_runtime_resume(struct device *dev) |
| 214 | { |
| 215 | struct i2c_client *client = container_of(dev, struct i2c_client, dev); |
| 216 | struct lis3lv02d *lis3 = i2c_get_clientdata(client); |
| 217 | |
| 218 | lis3lv02d_poweron(lis3); |
| 219 | return 0; |
| 220 | } |
| 221 | |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 222 | static const struct i2c_device_id lis3lv02d_id[] = { |
| 223 | {"lis3lv02d", 0 }, |
| 224 | {} |
| 225 | }; |
| 226 | |
| 227 | MODULE_DEVICE_TABLE(i2c, lis3lv02d_id); |
| 228 | |
Samu Onkalo | 2a34699 | 2010-10-22 07:57:23 -0400 | [diff] [blame] | 229 | static const struct dev_pm_ops lis3_pm_ops = { |
| 230 | SET_SYSTEM_SLEEP_PM_OPS(lis3lv02d_i2c_suspend, |
| 231 | lis3lv02d_i2c_resume) |
| 232 | SET_RUNTIME_PM_OPS(lis3_i2c_runtime_suspend, |
| 233 | lis3_i2c_runtime_resume, |
| 234 | NULL) |
| 235 | }; |
| 236 | |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 237 | static struct i2c_driver lis3lv02d_i2c_driver = { |
| 238 | .driver = { |
| 239 | .name = DRV_NAME, |
| 240 | .owner = THIS_MODULE, |
Samu Onkalo | 2a34699 | 2010-10-22 07:57:23 -0400 | [diff] [blame] | 241 | .pm = &lis3_pm_ops, |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 242 | }, |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 243 | .probe = lis3lv02d_i2c_probe, |
| 244 | .remove = __devexit_p(lis3lv02d_i2c_remove), |
| 245 | .id_table = lis3lv02d_id, |
| 246 | }; |
| 247 | |
| 248 | static int __init lis3lv02d_init(void) |
| 249 | { |
| 250 | return i2c_add_driver(&lis3lv02d_i2c_driver); |
| 251 | } |
| 252 | |
| 253 | static void __exit lis3lv02d_exit(void) |
| 254 | { |
| 255 | i2c_del_driver(&lis3lv02d_i2c_driver); |
| 256 | } |
| 257 | |
| 258 | MODULE_AUTHOR("Nokia Corporation"); |
| 259 | MODULE_DESCRIPTION("lis3lv02d I2C interface"); |
| 260 | MODULE_LICENSE("GPL"); |
| 261 | |
| 262 | module_init(lis3lv02d_init); |
| 263 | module_exit(lis3lv02d_exit); |