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