Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * ROHM 1780GLI Ambient Light Sensor Driver
0004  *
0005  * Copyright (C) 2016 Linaro Ltd.
0006  * Author: Linus Walleij <linus.walleij@linaro.org>
0007  * Loosely based on the previous BH1780 ALS misc driver
0008  * Copyright (C) 2010 Texas Instruments
0009  * Author: Hemanth V <hemanthv@ti.com>
0010  */
0011 #include <linux/i2c.h>
0012 #include <linux/slab.h>
0013 #include <linux/platform_device.h>
0014 #include <linux/delay.h>
0015 #include <linux/module.h>
0016 #include <linux/mod_devicetable.h>
0017 #include <linux/pm_runtime.h>
0018 #include <linux/iio/iio.h>
0019 #include <linux/iio/sysfs.h>
0020 #include <linux/bitops.h>
0021 
0022 #define BH1780_CMD_BIT      BIT(7)
0023 #define BH1780_REG_CONTROL  0x00
0024 #define BH1780_REG_PARTID   0x0A
0025 #define BH1780_REG_MANFID   0x0B
0026 #define BH1780_REG_DLOW     0x0C
0027 #define BH1780_REG_DHIGH    0x0D
0028 
0029 #define BH1780_REVMASK      GENMASK(3,0)
0030 #define BH1780_POWMASK      GENMASK(1,0)
0031 #define BH1780_POFF     (0x0)
0032 #define BH1780_PON      (0x3)
0033 
0034 /* power on settling time in ms */
0035 #define BH1780_PON_DELAY    2
0036 /* max time before value available in ms */
0037 #define BH1780_INTERVAL     250
0038 
0039 struct bh1780_data {
0040     struct i2c_client *client;
0041 };
0042 
0043 static int bh1780_write(struct bh1780_data *bh1780, u8 reg, u8 val)
0044 {
0045     int ret = i2c_smbus_write_byte_data(bh1780->client,
0046                         BH1780_CMD_BIT | reg,
0047                         val);
0048     if (ret < 0)
0049         dev_err(&bh1780->client->dev,
0050             "i2c_smbus_write_byte_data failed error "
0051             "%d, register %01x\n",
0052             ret, reg);
0053     return ret;
0054 }
0055 
0056 static int bh1780_read(struct bh1780_data *bh1780, u8 reg)
0057 {
0058     int ret = i2c_smbus_read_byte_data(bh1780->client,
0059                        BH1780_CMD_BIT | reg);
0060     if (ret < 0)
0061         dev_err(&bh1780->client->dev,
0062             "i2c_smbus_read_byte_data failed error "
0063             "%d, register %01x\n",
0064             ret, reg);
0065     return ret;
0066 }
0067 
0068 static int bh1780_read_word(struct bh1780_data *bh1780, u8 reg)
0069 {
0070     int ret = i2c_smbus_read_word_data(bh1780->client,
0071                        BH1780_CMD_BIT | reg);
0072     if (ret < 0)
0073         dev_err(&bh1780->client->dev,
0074             "i2c_smbus_read_word_data failed error "
0075             "%d, register %01x\n",
0076             ret, reg);
0077     return ret;
0078 }
0079 
0080 static int bh1780_debugfs_reg_access(struct iio_dev *indio_dev,
0081                   unsigned int reg, unsigned int writeval,
0082                   unsigned int *readval)
0083 {
0084     struct bh1780_data *bh1780 = iio_priv(indio_dev);
0085     int ret;
0086 
0087     if (!readval)
0088         return bh1780_write(bh1780, (u8)reg, (u8)writeval);
0089 
0090     ret = bh1780_read(bh1780, (u8)reg);
0091     if (ret < 0)
0092         return ret;
0093 
0094     *readval = ret;
0095 
0096     return 0;
0097 }
0098 
0099 static int bh1780_read_raw(struct iio_dev *indio_dev,
0100                struct iio_chan_spec const *chan,
0101                int *val, int *val2, long mask)
0102 {
0103     struct bh1780_data *bh1780 = iio_priv(indio_dev);
0104     int value;
0105 
0106     switch (mask) {
0107     case IIO_CHAN_INFO_RAW:
0108         switch (chan->type) {
0109         case IIO_LIGHT:
0110             pm_runtime_get_sync(&bh1780->client->dev);
0111             value = bh1780_read_word(bh1780, BH1780_REG_DLOW);
0112             if (value < 0)
0113                 return value;
0114             pm_runtime_mark_last_busy(&bh1780->client->dev);
0115             pm_runtime_put_autosuspend(&bh1780->client->dev);
0116             *val = value;
0117 
0118             return IIO_VAL_INT;
0119         default:
0120             return -EINVAL;
0121         }
0122     case IIO_CHAN_INFO_INT_TIME:
0123         *val = 0;
0124         *val2 = BH1780_INTERVAL * 1000;
0125         return IIO_VAL_INT_PLUS_MICRO;
0126     default:
0127         return -EINVAL;
0128     }
0129 }
0130 
0131 static const struct iio_info bh1780_info = {
0132     .read_raw = bh1780_read_raw,
0133     .debugfs_reg_access = bh1780_debugfs_reg_access,
0134 };
0135 
0136 static const struct iio_chan_spec bh1780_channels[] = {
0137     {
0138         .type = IIO_LIGHT,
0139         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
0140                       BIT(IIO_CHAN_INFO_INT_TIME)
0141     }
0142 };
0143 
0144 static int bh1780_probe(struct i2c_client *client,
0145             const struct i2c_device_id *id)
0146 {
0147     int ret;
0148     struct bh1780_data *bh1780;
0149     struct i2c_adapter *adapter = client->adapter;
0150     struct iio_dev *indio_dev;
0151 
0152     if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
0153         return -EIO;
0154 
0155     indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*bh1780));
0156     if (!indio_dev)
0157         return -ENOMEM;
0158 
0159     bh1780 = iio_priv(indio_dev);
0160     bh1780->client = client;
0161     i2c_set_clientdata(client, indio_dev);
0162 
0163     /* Power up the device */
0164     ret = bh1780_write(bh1780, BH1780_REG_CONTROL, BH1780_PON);
0165     if (ret < 0)
0166         return ret;
0167     msleep(BH1780_PON_DELAY);
0168     pm_runtime_get_noresume(&client->dev);
0169     pm_runtime_set_active(&client->dev);
0170     pm_runtime_enable(&client->dev);
0171 
0172     ret = bh1780_read(bh1780, BH1780_REG_PARTID);
0173     if (ret < 0)
0174         goto out_disable_pm;
0175     dev_info(&client->dev,
0176          "Ambient Light Sensor, Rev : %lu\n",
0177          (ret & BH1780_REVMASK));
0178 
0179     /*
0180      * As the device takes 250 ms to even come up with a fresh
0181      * measurement after power-on, do not shut it down unnecessarily.
0182      * Set autosuspend to a five seconds.
0183      */
0184     pm_runtime_set_autosuspend_delay(&client->dev, 5000);
0185     pm_runtime_use_autosuspend(&client->dev);
0186     pm_runtime_put(&client->dev);
0187 
0188     indio_dev->info = &bh1780_info;
0189     indio_dev->name = "bh1780";
0190     indio_dev->channels = bh1780_channels;
0191     indio_dev->num_channels = ARRAY_SIZE(bh1780_channels);
0192     indio_dev->modes = INDIO_DIRECT_MODE;
0193 
0194     ret = iio_device_register(indio_dev);
0195     if (ret)
0196         goto out_disable_pm;
0197     return 0;
0198 
0199 out_disable_pm:
0200     pm_runtime_put_noidle(&client->dev);
0201     pm_runtime_disable(&client->dev);
0202     return ret;
0203 }
0204 
0205 static int bh1780_remove(struct i2c_client *client)
0206 {
0207     struct iio_dev *indio_dev = i2c_get_clientdata(client);
0208     struct bh1780_data *bh1780 = iio_priv(indio_dev);
0209     int ret;
0210 
0211     iio_device_unregister(indio_dev);
0212     pm_runtime_get_sync(&client->dev);
0213     pm_runtime_put_noidle(&client->dev);
0214     pm_runtime_disable(&client->dev);
0215     ret = bh1780_write(bh1780, BH1780_REG_CONTROL, BH1780_POFF);
0216     if (ret < 0)
0217         dev_err(&client->dev, "failed to power off (%pe)\n",
0218             ERR_PTR(ret));
0219 
0220     return 0;
0221 }
0222 
0223 static int bh1780_runtime_suspend(struct device *dev)
0224 {
0225     struct i2c_client *client = to_i2c_client(dev);
0226     struct iio_dev *indio_dev = i2c_get_clientdata(client);
0227     struct bh1780_data *bh1780 = iio_priv(indio_dev);
0228     int ret;
0229 
0230     ret = bh1780_write(bh1780, BH1780_REG_CONTROL, BH1780_POFF);
0231     if (ret < 0) {
0232         dev_err(dev, "failed to runtime suspend\n");
0233         return ret;
0234     }
0235 
0236     return 0;
0237 }
0238 
0239 static int bh1780_runtime_resume(struct device *dev)
0240 {
0241     struct i2c_client *client = to_i2c_client(dev);
0242     struct iio_dev *indio_dev = i2c_get_clientdata(client);
0243     struct bh1780_data *bh1780 = iio_priv(indio_dev);
0244     int ret;
0245 
0246     ret = bh1780_write(bh1780, BH1780_REG_CONTROL, BH1780_PON);
0247     if (ret < 0) {
0248         dev_err(dev, "failed to runtime resume\n");
0249         return ret;
0250     }
0251 
0252     /* Wait for power on, then for a value to be available */
0253     msleep(BH1780_PON_DELAY + BH1780_INTERVAL);
0254 
0255     return 0;
0256 }
0257 
0258 static DEFINE_RUNTIME_DEV_PM_OPS(bh1780_dev_pm_ops, bh1780_runtime_suspend,
0259                 bh1780_runtime_resume, NULL);
0260 
0261 static const struct i2c_device_id bh1780_id[] = {
0262     { "bh1780", 0 },
0263     { },
0264 };
0265 
0266 MODULE_DEVICE_TABLE(i2c, bh1780_id);
0267 
0268 static const struct of_device_id of_bh1780_match[] = {
0269     { .compatible = "rohm,bh1780gli", },
0270     {},
0271 };
0272 MODULE_DEVICE_TABLE(of, of_bh1780_match);
0273 
0274 static struct i2c_driver bh1780_driver = {
0275     .probe      = bh1780_probe,
0276     .remove     = bh1780_remove,
0277     .id_table   = bh1780_id,
0278     .driver = {
0279         .name = "bh1780",
0280         .pm = pm_ptr(&bh1780_dev_pm_ops),
0281         .of_match_table = of_bh1780_match,
0282     },
0283 };
0284 
0285 module_i2c_driver(bh1780_driver);
0286 
0287 MODULE_DESCRIPTION("ROHM BH1780GLI Ambient Light Sensor Driver");
0288 MODULE_LICENSE("GPL");
0289 MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");