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 | |
Jean Delvare | ff60667 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 36 | #define DRV_NAME "lis3lv02d_i2c" |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 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 | |
Samu Onkalo | f10a540 | 2010-10-22 07:57:31 -0400 | [diff] [blame] | 69 | static inline s32 lis3_i2c_blockread(struct lis3lv02d *lis3, int reg, int len, |
| 70 | u8 *v) |
| 71 | { |
| 72 | struct i2c_client *c = lis3->bus_priv; |
| 73 | reg |= (1 << 7); /* 7th bit enables address auto incrementation */ |
| 74 | return i2c_smbus_read_i2c_block_data(c, reg, len, v); |
| 75 | } |
| 76 | |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 77 | static int lis3_i2c_init(struct lis3lv02d *lis3) |
| 78 | { |
| 79 | u8 reg; |
| 80 | int ret; |
| 81 | |
Mark Brown | ec400c9 | 2011-10-31 17:11:07 -0700 | [diff] [blame] | 82 | lis3_reg_ctrl(lis3, LIS3_REG_ON); |
Samu Onkalo | f9deb41 | 2010-10-22 07:57:24 -0400 | [diff] [blame] | 83 | |
| 84 | lis3->read(lis3, WHO_AM_I, ®); |
| 85 | if (reg != lis3->whoami) |
| 86 | printk(KERN_ERR "lis3: power on failure\n"); |
| 87 | |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 88 | /* power up the device */ |
| 89 | ret = lis3->read(lis3, CTRL_REG1, ®); |
| 90 | if (ret < 0) |
| 91 | return ret; |
| 92 | |
AnilKumar Ch | 0bf5a8b | 2012-08-22 12:00:39 +0530 | [diff] [blame^] | 93 | if (lis3->whoami == WAI_3DLH) |
| 94 | reg |= CTRL1_PM0 | CTRL1_Xen | CTRL1_Yen | CTRL1_Zen; |
| 95 | else |
| 96 | reg |= CTRL1_PD0 | CTRL1_Xen | CTRL1_Yen | CTRL1_Zen; |
| 97 | |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 98 | return lis3->write(lis3, CTRL_REG1, reg); |
| 99 | } |
| 100 | |
| 101 | /* Default axis mapping but it can be overwritten by platform data */ |
Takashi Iwai | 2ee3214 | 2010-10-01 17:14:25 -0400 | [diff] [blame] | 102 | static union axis_conversion lis3lv02d_axis_map = |
| 103 | { .as_array = { LIS3_DEV_X, LIS3_DEV_Y, LIS3_DEV_Z } }; |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 104 | |
| 105 | static int __devinit lis3lv02d_i2c_probe(struct i2c_client *client, |
| 106 | const struct i2c_device_id *id) |
| 107 | { |
| 108 | int ret = 0; |
| 109 | struct lis3lv02d_platform_data *pdata = client->dev.platform_data; |
| 110 | |
| 111 | if (pdata) { |
Samu Onkalo | f10a540 | 2010-10-22 07:57:31 -0400 | [diff] [blame] | 112 | if ((pdata->driver_features & LIS3_USE_BLOCK_READ) && |
| 113 | (i2c_check_functionality(client->adapter, |
| 114 | I2C_FUNC_SMBUS_I2C_BLOCK))) |
| 115 | lis3_dev.blkread = lis3_i2c_blockread; |
| 116 | |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 117 | if (pdata->axis_x) |
| 118 | lis3lv02d_axis_map.x = pdata->axis_x; |
| 119 | |
| 120 | if (pdata->axis_y) |
| 121 | lis3lv02d_axis_map.y = pdata->axis_y; |
| 122 | |
| 123 | if (pdata->axis_z) |
| 124 | lis3lv02d_axis_map.z = pdata->axis_z; |
| 125 | |
| 126 | if (pdata->setup_resources) |
| 127 | ret = pdata->setup_resources(); |
| 128 | |
| 129 | if (ret) |
| 130 | goto fail; |
| 131 | } |
| 132 | |
Mark Brown | ec400c9 | 2011-10-31 17:11:07 -0700 | [diff] [blame] | 133 | lis3_dev.regulators[0].supply = reg_vdd; |
| 134 | lis3_dev.regulators[1].supply = reg_vdd_io; |
| 135 | ret = regulator_bulk_get(&client->dev, |
| 136 | ARRAY_SIZE(lis3_dev.regulators), |
| 137 | lis3_dev.regulators); |
| 138 | if (ret < 0) |
| 139 | goto fail; |
Samu Onkalo | f9deb41 | 2010-10-22 07:57:24 -0400 | [diff] [blame] | 140 | |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 141 | lis3_dev.pdata = pdata; |
| 142 | lis3_dev.bus_priv = client; |
| 143 | lis3_dev.init = lis3_i2c_init; |
| 144 | lis3_dev.read = lis3_i2c_read; |
| 145 | lis3_dev.write = lis3_i2c_write; |
| 146 | lis3_dev.irq = client->irq; |
| 147 | lis3_dev.ac = lis3lv02d_axis_map; |
Samu Onkalo | 2a34699 | 2010-10-22 07:57:23 -0400 | [diff] [blame] | 148 | lis3_dev.pm_dev = &client->dev; |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 149 | |
| 150 | i2c_set_clientdata(client, &lis3_dev); |
Samu Onkalo | f9deb41 | 2010-10-22 07:57:24 -0400 | [diff] [blame] | 151 | |
| 152 | /* Provide power over the init call */ |
Mark Brown | ec400c9 | 2011-10-31 17:11:07 -0700 | [diff] [blame] | 153 | lis3_reg_ctrl(&lis3_dev, LIS3_REG_ON); |
Samu Onkalo | f9deb41 | 2010-10-22 07:57:24 -0400 | [diff] [blame] | 154 | |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 155 | ret = lis3lv02d_init_device(&lis3_dev); |
Samu Onkalo | f9deb41 | 2010-10-22 07:57:24 -0400 | [diff] [blame] | 156 | |
Mark Brown | ec400c9 | 2011-10-31 17:11:07 -0700 | [diff] [blame] | 157 | lis3_reg_ctrl(&lis3_dev, LIS3_REG_OFF); |
Samu Onkalo | b11e7b3 | 2010-10-22 07:57:34 -0400 | [diff] [blame] | 158 | |
Éric Piel | 0021586 | 2011-10-31 17:10:54 -0700 | [diff] [blame] | 159 | if (ret) |
| 160 | goto fail2; |
| 161 | return 0; |
| 162 | |
| 163 | fail2: |
| 164 | regulator_bulk_free(ARRAY_SIZE(lis3_dev.regulators), |
| 165 | lis3_dev.regulators); |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 166 | fail: |
Samu Onkalo | b11e7b3 | 2010-10-22 07:57:34 -0400 | [diff] [blame] | 167 | if (pdata && pdata->release_resources) |
| 168 | pdata->release_resources(); |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 169 | return ret; |
| 170 | } |
| 171 | |
| 172 | static int __devexit lis3lv02d_i2c_remove(struct i2c_client *client) |
| 173 | { |
Samu Onkalo | f9deb41 | 2010-10-22 07:57:24 -0400 | [diff] [blame] | 174 | struct lis3lv02d *lis3 = i2c_get_clientdata(client); |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 175 | struct lis3lv02d_platform_data *pdata = client->dev.platform_data; |
| 176 | |
| 177 | if (pdata && pdata->release_resources) |
| 178 | pdata->release_resources(); |
| 179 | |
Éric Piel | e1e5687 | 2011-10-31 17:11:02 -0700 | [diff] [blame] | 180 | lis3lv02d_joystick_disable(lis3); |
Samu Onkalo | f9deb41 | 2010-10-22 07:57:24 -0400 | [diff] [blame] | 181 | lis3lv02d_remove_fs(&lis3_dev); |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 182 | |
Mark Brown | ec400c9 | 2011-10-31 17:11:07 -0700 | [diff] [blame] | 183 | regulator_bulk_free(ARRAY_SIZE(lis3->regulators), |
| 184 | lis3_dev.regulators); |
Samu Onkalo | f9deb41 | 2010-10-22 07:57:24 -0400 | [diff] [blame] | 185 | return 0; |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Guenter Roeck | 32292f4 | 2010-11-18 21:06:45 -0800 | [diff] [blame] | 188 | #ifdef CONFIG_PM_SLEEP |
Samu Onkalo | 2a34699 | 2010-10-22 07:57:23 -0400 | [diff] [blame] | 189 | static int lis3lv02d_i2c_suspend(struct device *dev) |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 190 | { |
Samu Onkalo | 2a34699 | 2010-10-22 07:57:23 -0400 | [diff] [blame] | 191 | struct i2c_client *client = container_of(dev, struct i2c_client, dev); |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 192 | struct lis3lv02d *lis3 = i2c_get_clientdata(client); |
| 193 | |
Kuninori Morimoto | 5facb09 | 2010-09-17 17:24:10 +0200 | [diff] [blame] | 194 | if (!lis3->pdata || !lis3->pdata->wakeup_flags) |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 195 | lis3lv02d_poweroff(lis3); |
| 196 | return 0; |
| 197 | } |
| 198 | |
Samu Onkalo | 2a34699 | 2010-10-22 07:57:23 -0400 | [diff] [blame] | 199 | static int lis3lv02d_i2c_resume(struct device *dev) |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 200 | { |
Samu Onkalo | 2a34699 | 2010-10-22 07:57:23 -0400 | [diff] [blame] | 201 | struct i2c_client *client = container_of(dev, struct i2c_client, dev); |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 202 | struct lis3lv02d *lis3 = i2c_get_clientdata(client); |
| 203 | |
Samu Onkalo | 2a34699 | 2010-10-22 07:57:23 -0400 | [diff] [blame] | 204 | /* |
| 205 | * pm_runtime documentation says that devices should always |
| 206 | * be powered on at resume. Pm_runtime turns them off after system |
| 207 | * wide resume is complete. |
| 208 | */ |
| 209 | if (!lis3->pdata || !lis3->pdata->wakeup_flags || |
| 210 | pm_runtime_suspended(dev)) |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 211 | lis3lv02d_poweron(lis3); |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 212 | |
Samu Onkalo | 2a34699 | 2010-10-22 07:57:23 -0400 | [diff] [blame] | 213 | return 0; |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 214 | } |
Guenter Roeck | 32292f4 | 2010-11-18 21:06:45 -0800 | [diff] [blame] | 215 | #endif /* CONFIG_PM_SLEEP */ |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 216 | |
Guenter Roeck | 32292f4 | 2010-11-18 21:06:45 -0800 | [diff] [blame] | 217 | #ifdef CONFIG_PM_RUNTIME |
Samu Onkalo | 2a34699 | 2010-10-22 07:57:23 -0400 | [diff] [blame] | 218 | static int lis3_i2c_runtime_suspend(struct device *dev) |
| 219 | { |
| 220 | struct i2c_client *client = container_of(dev, struct i2c_client, dev); |
| 221 | struct lis3lv02d *lis3 = i2c_get_clientdata(client); |
| 222 | |
| 223 | lis3lv02d_poweroff(lis3); |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | static int lis3_i2c_runtime_resume(struct device *dev) |
| 228 | { |
| 229 | struct i2c_client *client = container_of(dev, struct i2c_client, dev); |
| 230 | struct lis3lv02d *lis3 = i2c_get_clientdata(client); |
| 231 | |
| 232 | lis3lv02d_poweron(lis3); |
| 233 | return 0; |
| 234 | } |
Guenter Roeck | 32292f4 | 2010-11-18 21:06:45 -0800 | [diff] [blame] | 235 | #endif /* CONFIG_PM_RUNTIME */ |
Samu Onkalo | 2a34699 | 2010-10-22 07:57:23 -0400 | [diff] [blame] | 236 | |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 237 | static const struct i2c_device_id lis3lv02d_id[] = { |
| 238 | {"lis3lv02d", 0 }, |
AnilKumar Ch | 0bf5a8b | 2012-08-22 12:00:39 +0530 | [diff] [blame^] | 239 | {"lis331dlh", LIS331DLH}, |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 240 | {} |
| 241 | }; |
| 242 | |
| 243 | MODULE_DEVICE_TABLE(i2c, lis3lv02d_id); |
| 244 | |
Samu Onkalo | 2a34699 | 2010-10-22 07:57:23 -0400 | [diff] [blame] | 245 | static const struct dev_pm_ops lis3_pm_ops = { |
| 246 | SET_SYSTEM_SLEEP_PM_OPS(lis3lv02d_i2c_suspend, |
| 247 | lis3lv02d_i2c_resume) |
| 248 | SET_RUNTIME_PM_OPS(lis3_i2c_runtime_suspend, |
| 249 | lis3_i2c_runtime_resume, |
| 250 | NULL) |
| 251 | }; |
| 252 | |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 253 | static struct i2c_driver lis3lv02d_i2c_driver = { |
| 254 | .driver = { |
| 255 | .name = DRV_NAME, |
| 256 | .owner = THIS_MODULE, |
Samu Onkalo | 2a34699 | 2010-10-22 07:57:23 -0400 | [diff] [blame] | 257 | .pm = &lis3_pm_ops, |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 258 | }, |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 259 | .probe = lis3lv02d_i2c_probe, |
| 260 | .remove = __devexit_p(lis3lv02d_i2c_remove), |
| 261 | .id_table = lis3lv02d_id, |
| 262 | }; |
| 263 | |
Axel Lin | a64fe2e | 2012-01-22 15:36:45 +0800 | [diff] [blame] | 264 | module_i2c_driver(lis3lv02d_i2c_driver); |
Samu Onkalo | 312ea07 | 2009-12-17 15:27:07 -0800 | [diff] [blame] | 265 | |
| 266 | MODULE_AUTHOR("Nokia Corporation"); |
| 267 | MODULE_DESCRIPTION("lis3lv02d I2C interface"); |
| 268 | MODULE_LICENSE("GPL"); |