root/drivers/iio/accel/mma7660.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. mma7660_set_mode
  2. mma7660_read_accel
  3. mma7660_read_raw
  4. mma7660_probe
  5. mma7660_remove
  6. mma7660_suspend
  7. mma7660_resume

   1 // SPDX-License-Identifier: GPL-2.0-only
   2 /**
   3  * Freescale MMA7660FC 3-Axis Accelerometer
   4  *
   5  * Copyright (c) 2016, Intel Corporation.
   6  *
   7  * IIO driver for Freescale MMA7660FC; 7-bit I2C address: 0x4c.
   8  */
   9 
  10 #include <linux/acpi.h>
  11 #include <linux/i2c.h>
  12 #include <linux/module.h>
  13 #include <linux/iio/iio.h>
  14 #include <linux/iio/sysfs.h>
  15 
  16 #define MMA7660_DRIVER_NAME     "mma7660"
  17 
  18 #define MMA7660_REG_XOUT        0x00
  19 #define MMA7660_REG_YOUT        0x01
  20 #define MMA7660_REG_ZOUT        0x02
  21 #define MMA7660_REG_OUT_BIT_ALERT       BIT(6)
  22 
  23 #define MMA7660_REG_MODE        0x07
  24 #define MMA7660_REG_MODE_BIT_MODE       BIT(0)
  25 #define MMA7660_REG_MODE_BIT_TON        BIT(2)
  26 
  27 #define MMA7660_I2C_READ_RETRIES        5
  28 
  29 /*
  30  * The accelerometer has one measurement range:
  31  *
  32  * -1.5g - +1.5g (6-bit, signed)
  33  *
  34  * scale = (1.5 + 1.5) * 9.81 / (2^6 - 1)       = 0.467142857
  35  */
  36 
  37 #define MMA7660_SCALE_AVAIL     "0.467142857"
  38 
  39 static const int mma7660_nscale = 467142857;
  40 
  41 #define MMA7660_CHANNEL(reg, axis) {    \
  42         .type = IIO_ACCEL,      \
  43         .address = reg, \
  44         .modified = 1,  \
  45         .channel2 = IIO_MOD_##axis,     \
  46         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),   \
  47         .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),   \
  48 }
  49 
  50 static const struct iio_chan_spec mma7660_channels[] = {
  51         MMA7660_CHANNEL(MMA7660_REG_XOUT, X),
  52         MMA7660_CHANNEL(MMA7660_REG_YOUT, Y),
  53         MMA7660_CHANNEL(MMA7660_REG_ZOUT, Z),
  54 };
  55 
  56 enum mma7660_mode {
  57         MMA7660_MODE_STANDBY,
  58         MMA7660_MODE_ACTIVE
  59 };
  60 
  61 struct mma7660_data {
  62         struct i2c_client *client;
  63         struct mutex lock;
  64         enum mma7660_mode mode;
  65 };
  66 
  67 static IIO_CONST_ATTR(in_accel_scale_available, MMA7660_SCALE_AVAIL);
  68 
  69 static struct attribute *mma7660_attributes[] = {
  70         &iio_const_attr_in_accel_scale_available.dev_attr.attr,
  71         NULL,
  72 };
  73 
  74 static const struct attribute_group mma7660_attribute_group = {
  75         .attrs = mma7660_attributes
  76 };
  77 
  78 static int mma7660_set_mode(struct mma7660_data *data,
  79                                 enum mma7660_mode mode)
  80 {
  81         int ret;
  82         struct i2c_client *client = data->client;
  83 
  84         if (mode == data->mode)
  85                 return 0;
  86 
  87         ret = i2c_smbus_read_byte_data(client, MMA7660_REG_MODE);
  88         if (ret < 0) {
  89                 dev_err(&client->dev, "failed to read sensor mode\n");
  90                 return ret;
  91         }
  92 
  93         if (mode == MMA7660_MODE_ACTIVE) {
  94                 ret &= ~MMA7660_REG_MODE_BIT_TON;
  95                 ret |= MMA7660_REG_MODE_BIT_MODE;
  96         } else {
  97                 ret &= ~MMA7660_REG_MODE_BIT_TON;
  98                 ret &= ~MMA7660_REG_MODE_BIT_MODE;
  99         }
 100 
 101         ret = i2c_smbus_write_byte_data(client, MMA7660_REG_MODE, ret);
 102         if (ret < 0) {
 103                 dev_err(&client->dev, "failed to change sensor mode\n");
 104                 return ret;
 105         }
 106 
 107         data->mode = mode;
 108 
 109         return ret;
 110 }
 111 
 112 static int mma7660_read_accel(struct mma7660_data *data, u8 address)
 113 {
 114         int ret, retries = MMA7660_I2C_READ_RETRIES;
 115         struct i2c_client *client = data->client;
 116 
 117         /*
 118          * Read data. If the Alert bit is set, the register was read at
 119          * the same time as the device was attempting to update the content.
 120          * The solution is to read the register again. Do this only
 121          * MMA7660_I2C_READ_RETRIES times to avoid spending too much time
 122          * in the kernel.
 123          */
 124         do {
 125                 ret = i2c_smbus_read_byte_data(client, address);
 126                 if (ret < 0) {
 127                         dev_err(&client->dev, "register read failed\n");
 128                         return ret;
 129                 }
 130         } while (retries-- > 0 && ret & MMA7660_REG_OUT_BIT_ALERT);
 131 
 132         if (ret & MMA7660_REG_OUT_BIT_ALERT) {
 133                 dev_err(&client->dev, "all register read retries failed\n");
 134                 return -ETIMEDOUT;
 135         }
 136 
 137         return ret;
 138 }
 139 
 140 static int mma7660_read_raw(struct iio_dev *indio_dev,
 141                                 struct iio_chan_spec const *chan,
 142                                 int *val, int *val2, long mask)
 143 {
 144         struct mma7660_data *data = iio_priv(indio_dev);
 145         int ret;
 146 
 147         switch (mask) {
 148         case IIO_CHAN_INFO_RAW:
 149                 mutex_lock(&data->lock);
 150                 ret = mma7660_read_accel(data, chan->address);
 151                 mutex_unlock(&data->lock);
 152                 if (ret < 0)
 153                         return ret;
 154                 *val = sign_extend32(ret, 5);
 155                 return IIO_VAL_INT;
 156         case IIO_CHAN_INFO_SCALE:
 157                 *val = 0;
 158                 *val2 = mma7660_nscale;
 159                 return IIO_VAL_INT_PLUS_NANO;
 160         default:
 161                 return -EINVAL;
 162         }
 163 
 164         return -EINVAL;
 165 }
 166 
 167 static const struct iio_info mma7660_info = {
 168         .read_raw               = mma7660_read_raw,
 169         .attrs                  = &mma7660_attribute_group,
 170 };
 171 
 172 static int mma7660_probe(struct i2c_client *client,
 173                         const struct i2c_device_id *id)
 174 {
 175         int ret;
 176         struct iio_dev *indio_dev;
 177         struct mma7660_data *data;
 178 
 179         indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
 180         if (!indio_dev) {
 181                 dev_err(&client->dev, "iio allocation failed!\n");
 182                 return -ENOMEM;
 183         }
 184 
 185         data = iio_priv(indio_dev);
 186         data->client = client;
 187         i2c_set_clientdata(client, indio_dev);
 188         mutex_init(&data->lock);
 189         data->mode = MMA7660_MODE_STANDBY;
 190 
 191         indio_dev->dev.parent = &client->dev;
 192         indio_dev->info = &mma7660_info;
 193         indio_dev->name = MMA7660_DRIVER_NAME;
 194         indio_dev->modes = INDIO_DIRECT_MODE;
 195         indio_dev->channels = mma7660_channels;
 196         indio_dev->num_channels = ARRAY_SIZE(mma7660_channels);
 197 
 198         ret = mma7660_set_mode(data, MMA7660_MODE_ACTIVE);
 199         if (ret < 0)
 200                 return ret;
 201 
 202         ret = iio_device_register(indio_dev);
 203         if (ret < 0) {
 204                 dev_err(&client->dev, "device_register failed\n");
 205                 mma7660_set_mode(data, MMA7660_MODE_STANDBY);
 206         }
 207 
 208         return ret;
 209 }
 210 
 211 static int mma7660_remove(struct i2c_client *client)
 212 {
 213         struct iio_dev *indio_dev = i2c_get_clientdata(client);
 214 
 215         iio_device_unregister(indio_dev);
 216 
 217         return mma7660_set_mode(iio_priv(indio_dev), MMA7660_MODE_STANDBY);
 218 }
 219 
 220 #ifdef CONFIG_PM_SLEEP
 221 static int mma7660_suspend(struct device *dev)
 222 {
 223         struct mma7660_data *data;
 224 
 225         data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
 226 
 227         return mma7660_set_mode(data, MMA7660_MODE_STANDBY);
 228 }
 229 
 230 static int mma7660_resume(struct device *dev)
 231 {
 232         struct mma7660_data *data;
 233 
 234         data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
 235 
 236         return mma7660_set_mode(data, MMA7660_MODE_ACTIVE);
 237 }
 238 
 239 static SIMPLE_DEV_PM_OPS(mma7660_pm_ops, mma7660_suspend, mma7660_resume);
 240 
 241 #define MMA7660_PM_OPS (&mma7660_pm_ops)
 242 #else
 243 #define MMA7660_PM_OPS NULL
 244 #endif
 245 
 246 static const struct i2c_device_id mma7660_i2c_id[] = {
 247         {"mma7660", 0},
 248         {}
 249 };
 250 MODULE_DEVICE_TABLE(i2c, mma7660_i2c_id);
 251 
 252 static const struct of_device_id mma7660_of_match[] = {
 253         { .compatible = "fsl,mma7660" },
 254         { }
 255 };
 256 MODULE_DEVICE_TABLE(of, mma7660_of_match);
 257 
 258 static const struct acpi_device_id mma7660_acpi_id[] = {
 259         {"MMA7660", 0},
 260         {}
 261 };
 262 
 263 MODULE_DEVICE_TABLE(acpi, mma7660_acpi_id);
 264 
 265 static struct i2c_driver mma7660_driver = {
 266         .driver = {
 267                 .name = "mma7660",
 268                 .pm = MMA7660_PM_OPS,
 269                 .of_match_table = mma7660_of_match,
 270                 .acpi_match_table = ACPI_PTR(mma7660_acpi_id),
 271         },
 272         .probe          = mma7660_probe,
 273         .remove         = mma7660_remove,
 274         .id_table       = mma7660_i2c_id,
 275 };
 276 
 277 module_i2c_driver(mma7660_driver);
 278 
 279 MODULE_AUTHOR("Constantin Musca <constantin.musca@intel.com>");
 280 MODULE_DESCRIPTION("Freescale MMA7660FC 3-Axis Accelerometer driver");
 281 MODULE_LICENSE("GPL v2");

/* [<][>][^][v][top][bottom][index][help] */