root/drivers/iio/light/cm3232.c

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

DEFINITIONS

This source file includes following definitions.
  1. cm3232_reg_init
  2. cm3232_read_als_it
  3. cm3232_write_als_it
  4. cm3232_get_lux
  5. cm3232_read_raw
  6. cm3232_write_raw
  7. cm3232_get_it_available
  8. cm3232_probe
  9. cm3232_remove
  10. cm3232_suspend
  11. cm3232_resume

   1 // SPDX-License-Identifier: GPL-2.0-only
   2 /*
   3  * CM3232 Ambient Light Sensor
   4  *
   5  * Copyright (C) 2014-2015 Capella Microsystems Inc.
   6  * Author: Kevin Tsai <ktsai@capellamicro.com>
   7  *
   8  * IIO driver for CM3232 (7-bit I2C slave address 0x10).
   9  */
  10 
  11 #include <linux/i2c.h>
  12 #include <linux/module.h>
  13 #include <linux/iio/iio.h>
  14 #include <linux/iio/sysfs.h>
  15 #include <linux/init.h>
  16 
  17 /* Registers Address */
  18 #define CM3232_REG_ADDR_CMD             0x00
  19 #define CM3232_REG_ADDR_ALS             0x50
  20 #define CM3232_REG_ADDR_ID              0x53
  21 
  22 #define CM3232_CMD_ALS_DISABLE          BIT(0)
  23 
  24 #define CM3232_CMD_ALS_IT_SHIFT         2
  25 #define CM3232_CMD_ALS_IT_MASK          (BIT(2) | BIT(3) | BIT(4))
  26 #define CM3232_CMD_ALS_IT_DEFAULT       (0x01 << CM3232_CMD_ALS_IT_SHIFT)
  27 
  28 #define CM3232_CMD_ALS_RESET            BIT(6)
  29 
  30 #define CM3232_CMD_DEFAULT              CM3232_CMD_ALS_IT_DEFAULT
  31 
  32 #define CM3232_HW_ID                    0x32
  33 #define CM3232_CALIBSCALE_DEFAULT       100000
  34 #define CM3232_CALIBSCALE_RESOLUTION    100000
  35 #define CM3232_MLUX_PER_LUX             1000
  36 
  37 #define CM3232_MLUX_PER_BIT_DEFAULT     64
  38 #define CM3232_MLUX_PER_BIT_BASE_IT     100000
  39 
  40 static const struct {
  41         int val;
  42         int val2;
  43         u8 it;
  44 } cm3232_als_it_scales[] = {
  45         {0, 100000, 0}, /* 0.100000 */
  46         {0, 200000, 1}, /* 0.200000 */
  47         {0, 400000, 2}, /* 0.400000 */
  48         {0, 800000, 3}, /* 0.800000 */
  49         {1, 600000, 4}, /* 1.600000 */
  50         {3, 200000, 5}, /* 3.200000 */
  51 };
  52 
  53 struct cm3232_als_info {
  54         u8 regs_cmd_default;
  55         u8 hw_id;
  56         int calibscale;
  57         int mlux_per_bit;
  58         int mlux_per_bit_base_it;
  59 };
  60 
  61 static struct cm3232_als_info cm3232_als_info_default = {
  62         .regs_cmd_default = CM3232_CMD_DEFAULT,
  63         .hw_id = CM3232_HW_ID,
  64         .calibscale = CM3232_CALIBSCALE_DEFAULT,
  65         .mlux_per_bit = CM3232_MLUX_PER_BIT_DEFAULT,
  66         .mlux_per_bit_base_it = CM3232_MLUX_PER_BIT_BASE_IT,
  67 };
  68 
  69 struct cm3232_chip {
  70         struct i2c_client *client;
  71         struct cm3232_als_info *als_info;
  72         u8 regs_cmd;
  73         u16 regs_als;
  74 };
  75 
  76 /**
  77  * cm3232_reg_init() - Initialize CM3232
  78  * @chip:       pointer of struct cm3232_chip.
  79  *
  80  * Check and initialize CM3232 ambient light sensor.
  81  *
  82  * Return: 0 for success; otherwise for error code.
  83  */
  84 static int cm3232_reg_init(struct cm3232_chip *chip)
  85 {
  86         struct i2c_client *client = chip->client;
  87         s32 ret;
  88 
  89         chip->als_info = &cm3232_als_info_default;
  90 
  91         /* Identify device */
  92         ret = i2c_smbus_read_word_data(client, CM3232_REG_ADDR_ID);
  93         if (ret < 0) {
  94                 dev_err(&chip->client->dev, "Error reading addr_id\n");
  95                 return ret;
  96         }
  97 
  98         if ((ret & 0xFF) != chip->als_info->hw_id)
  99                 return -ENODEV;
 100 
 101         /* Disable and reset device */
 102         chip->regs_cmd = CM3232_CMD_ALS_DISABLE | CM3232_CMD_ALS_RESET;
 103         ret = i2c_smbus_write_byte_data(client, CM3232_REG_ADDR_CMD,
 104                                         chip->regs_cmd);
 105         if (ret < 0) {
 106                 dev_err(&chip->client->dev, "Error writing reg_cmd\n");
 107                 return ret;
 108         }
 109 
 110         /* Register default value */
 111         chip->regs_cmd = chip->als_info->regs_cmd_default;
 112 
 113         /* Configure register */
 114         ret = i2c_smbus_write_byte_data(client, CM3232_REG_ADDR_CMD,
 115                                         chip->regs_cmd);
 116         if (ret < 0)
 117                 dev_err(&chip->client->dev, "Error writing reg_cmd\n");
 118 
 119         return ret;
 120 }
 121 
 122 /**
 123  *  cm3232_read_als_it() - Get sensor integration time
 124  *  @chip:      pointer of struct cm3232_chip
 125  *  @val:       pointer of int to load the integration (sec).
 126  *  @val2:      pointer of int to load the integration time (microsecond).
 127  *
 128  *  Report the current integration time.
 129  *
 130  *  Return: IIO_VAL_INT_PLUS_MICRO for success, otherwise -EINVAL.
 131  */
 132 static int cm3232_read_als_it(struct cm3232_chip *chip, int *val, int *val2)
 133 {
 134         u16 als_it;
 135         int i;
 136 
 137         als_it = chip->regs_cmd;
 138         als_it &= CM3232_CMD_ALS_IT_MASK;
 139         als_it >>= CM3232_CMD_ALS_IT_SHIFT;
 140         for (i = 0; i < ARRAY_SIZE(cm3232_als_it_scales); i++) {
 141                 if (als_it == cm3232_als_it_scales[i].it) {
 142                         *val = cm3232_als_it_scales[i].val;
 143                         *val2 = cm3232_als_it_scales[i].val2;
 144                         return IIO_VAL_INT_PLUS_MICRO;
 145                 }
 146         }
 147 
 148         return -EINVAL;
 149 }
 150 
 151 /**
 152  * cm3232_write_als_it() - Write sensor integration time
 153  * @chip:       pointer of struct cm3232_chip.
 154  * @val:        integration time in second.
 155  * @val2:       integration time in microsecond.
 156  *
 157  * Convert integration time to sensor value.
 158  *
 159  * Return: i2c_smbus_write_byte_data command return value.
 160  */
 161 static int cm3232_write_als_it(struct cm3232_chip *chip, int val, int val2)
 162 {
 163         struct i2c_client *client = chip->client;
 164         u16 als_it, cmd;
 165         int i;
 166         s32 ret;
 167 
 168         for (i = 0; i < ARRAY_SIZE(cm3232_als_it_scales); i++) {
 169                 if (val == cm3232_als_it_scales[i].val &&
 170                         val2 == cm3232_als_it_scales[i].val2) {
 171 
 172                         als_it = cm3232_als_it_scales[i].it;
 173                         als_it <<= CM3232_CMD_ALS_IT_SHIFT;
 174 
 175                         cmd = chip->regs_cmd & ~CM3232_CMD_ALS_IT_MASK;
 176                         cmd |= als_it;
 177                         ret = i2c_smbus_write_byte_data(client,
 178                                                         CM3232_REG_ADDR_CMD,
 179                                                         cmd);
 180                         if (ret < 0)
 181                                 return ret;
 182                         chip->regs_cmd = cmd;
 183                         return 0;
 184                 }
 185         }
 186         return -EINVAL;
 187 }
 188 
 189 /**
 190  * cm3232_get_lux() - report current lux value
 191  * @chip:       pointer of struct cm3232_chip.
 192  *
 193  * Convert sensor data to lux.  It depends on integration
 194  * time and calibscale variable.
 195  *
 196  * Return: Zero or positive value is lux, otherwise error code.
 197  */
 198 static int cm3232_get_lux(struct cm3232_chip *chip)
 199 {
 200         struct i2c_client *client = chip->client;
 201         struct cm3232_als_info *als_info = chip->als_info;
 202         int ret;
 203         int val, val2;
 204         int als_it;
 205         u64 lux;
 206 
 207         /* Calculate mlux per bit based on als_it */
 208         ret = cm3232_read_als_it(chip, &val, &val2);
 209         if (ret < 0)
 210                 return -EINVAL;
 211         als_it = val * 1000000 + val2;
 212         lux = (__force u64)als_info->mlux_per_bit;
 213         lux *= als_info->mlux_per_bit_base_it;
 214         lux = div_u64(lux, als_it);
 215 
 216         ret = i2c_smbus_read_word_data(client, CM3232_REG_ADDR_ALS);
 217         if (ret < 0) {
 218                 dev_err(&client->dev, "Error reading reg_addr_als\n");
 219                 return ret;
 220         }
 221 
 222         chip->regs_als = (u16)ret;
 223         lux *= chip->regs_als;
 224         lux *= als_info->calibscale;
 225         lux = div_u64(lux, CM3232_CALIBSCALE_RESOLUTION);
 226         lux = div_u64(lux, CM3232_MLUX_PER_LUX);
 227 
 228         if (lux > 0xFFFF)
 229                 lux = 0xFFFF;
 230 
 231         return (int)lux;
 232 }
 233 
 234 static int cm3232_read_raw(struct iio_dev *indio_dev,
 235                         struct iio_chan_spec const *chan,
 236                         int *val, int *val2, long mask)
 237 {
 238         struct cm3232_chip *chip = iio_priv(indio_dev);
 239         struct cm3232_als_info *als_info = chip->als_info;
 240         int ret;
 241 
 242         switch (mask) {
 243         case IIO_CHAN_INFO_PROCESSED:
 244                 ret = cm3232_get_lux(chip);
 245                 if (ret < 0)
 246                         return ret;
 247                 *val = ret;
 248                 return IIO_VAL_INT;
 249         case IIO_CHAN_INFO_CALIBSCALE:
 250                 *val = als_info->calibscale;
 251                 return IIO_VAL_INT;
 252         case IIO_CHAN_INFO_INT_TIME:
 253                 return cm3232_read_als_it(chip, val, val2);
 254         }
 255 
 256         return -EINVAL;
 257 }
 258 
 259 static int cm3232_write_raw(struct iio_dev *indio_dev,
 260                         struct iio_chan_spec const *chan,
 261                         int val, int val2, long mask)
 262 {
 263         struct cm3232_chip *chip = iio_priv(indio_dev);
 264         struct cm3232_als_info *als_info = chip->als_info;
 265 
 266         switch (mask) {
 267         case IIO_CHAN_INFO_CALIBSCALE:
 268                 als_info->calibscale = val;
 269                 return 0;
 270         case IIO_CHAN_INFO_INT_TIME:
 271                 return cm3232_write_als_it(chip, val, val2);
 272         }
 273 
 274         return -EINVAL;
 275 }
 276 
 277 /**
 278  * cm3232_get_it_available() - Get available ALS IT value
 279  * @dev:        pointer of struct device.
 280  * @attr:       pointer of struct device_attribute.
 281  * @buf:        pointer of return string buffer.
 282  *
 283  * Display the available integration time in second.
 284  *
 285  * Return: string length.
 286  */
 287 static ssize_t cm3232_get_it_available(struct device *dev,
 288                         struct device_attribute *attr, char *buf)
 289 {
 290         int i, len;
 291 
 292         for (i = 0, len = 0; i < ARRAY_SIZE(cm3232_als_it_scales); i++)
 293                 len += scnprintf(buf + len, PAGE_SIZE - len, "%u.%06u ",
 294                         cm3232_als_it_scales[i].val,
 295                         cm3232_als_it_scales[i].val2);
 296         return len + scnprintf(buf + len, PAGE_SIZE - len, "\n");
 297 }
 298 
 299 static const struct iio_chan_spec cm3232_channels[] = {
 300         {
 301                 .type = IIO_LIGHT,
 302                 .info_mask_separate =
 303                         BIT(IIO_CHAN_INFO_PROCESSED) |
 304                         BIT(IIO_CHAN_INFO_CALIBSCALE) |
 305                         BIT(IIO_CHAN_INFO_INT_TIME),
 306         }
 307 };
 308 
 309 static IIO_DEVICE_ATTR(in_illuminance_integration_time_available,
 310                         S_IRUGO, cm3232_get_it_available, NULL, 0);
 311 
 312 static struct attribute *cm3232_attributes[] = {
 313         &iio_dev_attr_in_illuminance_integration_time_available.dev_attr.attr,
 314         NULL,
 315 };
 316 
 317 static const struct attribute_group cm3232_attribute_group = {
 318         .attrs = cm3232_attributes
 319 };
 320 
 321 static const struct iio_info cm3232_info = {
 322         .read_raw               = &cm3232_read_raw,
 323         .write_raw              = &cm3232_write_raw,
 324         .attrs                  = &cm3232_attribute_group,
 325 };
 326 
 327 static int cm3232_probe(struct i2c_client *client,
 328                         const struct i2c_device_id *id)
 329 {
 330         struct cm3232_chip *chip;
 331         struct iio_dev *indio_dev;
 332         int ret;
 333 
 334         indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
 335         if (!indio_dev)
 336                 return -ENOMEM;
 337 
 338         chip = iio_priv(indio_dev);
 339         i2c_set_clientdata(client, indio_dev);
 340         chip->client = client;
 341 
 342         indio_dev->dev.parent = &client->dev;
 343         indio_dev->channels = cm3232_channels;
 344         indio_dev->num_channels = ARRAY_SIZE(cm3232_channels);
 345         indio_dev->info = &cm3232_info;
 346         indio_dev->name = id->name;
 347         indio_dev->modes = INDIO_DIRECT_MODE;
 348 
 349         ret = cm3232_reg_init(chip);
 350         if (ret) {
 351                 dev_err(&client->dev,
 352                         "%s: register init failed\n",
 353                         __func__);
 354                 return ret;
 355         }
 356 
 357         return iio_device_register(indio_dev);
 358 }
 359 
 360 static int cm3232_remove(struct i2c_client *client)
 361 {
 362         struct iio_dev *indio_dev = i2c_get_clientdata(client);
 363 
 364         i2c_smbus_write_byte_data(client, CM3232_REG_ADDR_CMD,
 365                 CM3232_CMD_ALS_DISABLE);
 366 
 367         iio_device_unregister(indio_dev);
 368 
 369         return 0;
 370 }
 371 
 372 static const struct i2c_device_id cm3232_id[] = {
 373         {"cm3232", 0},
 374         {}
 375 };
 376 
 377 #ifdef CONFIG_PM_SLEEP
 378 static int cm3232_suspend(struct device *dev)
 379 {
 380         struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
 381         struct cm3232_chip *chip = iio_priv(indio_dev);
 382         struct i2c_client *client = chip->client;
 383         int ret;
 384 
 385         chip->regs_cmd |= CM3232_CMD_ALS_DISABLE;
 386         ret = i2c_smbus_write_byte_data(client, CM3232_REG_ADDR_CMD,
 387                                         chip->regs_cmd);
 388 
 389         return ret;
 390 }
 391 
 392 static int cm3232_resume(struct device *dev)
 393 {
 394         struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
 395         struct cm3232_chip *chip = iio_priv(indio_dev);
 396         struct i2c_client *client = chip->client;
 397         int ret;
 398 
 399         chip->regs_cmd &= ~CM3232_CMD_ALS_DISABLE;
 400         ret = i2c_smbus_write_byte_data(client, CM3232_REG_ADDR_CMD,
 401                                         chip->regs_cmd | CM3232_CMD_ALS_RESET);
 402 
 403         return ret;
 404 }
 405 
 406 static const struct dev_pm_ops cm3232_pm_ops = {
 407         SET_SYSTEM_SLEEP_PM_OPS(cm3232_suspend, cm3232_resume)};
 408 #endif
 409 
 410 MODULE_DEVICE_TABLE(i2c, cm3232_id);
 411 
 412 static const struct of_device_id cm3232_of_match[] = {
 413         {.compatible = "capella,cm3232"},
 414         {}
 415 };
 416 MODULE_DEVICE_TABLE(of, cm3232_of_match);
 417 
 418 static struct i2c_driver cm3232_driver = {
 419         .driver = {
 420                 .name   = "cm3232",
 421                 .of_match_table = of_match_ptr(cm3232_of_match),
 422 #ifdef CONFIG_PM_SLEEP
 423                 .pm     = &cm3232_pm_ops,
 424 #endif
 425         },
 426         .id_table       = cm3232_id,
 427         .probe          = cm3232_probe,
 428         .remove         = cm3232_remove,
 429 };
 430 
 431 module_i2c_driver(cm3232_driver);
 432 
 433 MODULE_AUTHOR("Kevin Tsai <ktsai@capellamicro.com>");
 434 MODULE_DESCRIPTION("CM3232 ambient light sensor driver");
 435 MODULE_LICENSE("GPL");

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