^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * ROHM 1780GLI Ambient Light Sensor Driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2016 Linaro Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Author: Linus Walleij <linus.walleij@linaro.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Loosely based on the previous BH1780 ALS misc driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Copyright (C) 2010 Texas Instruments
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Author: Hemanth V <hemanthv@ti.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/mod_devicetable.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/pm_runtime.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/iio/iio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/iio/sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/bitops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define BH1780_CMD_BIT BIT(7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define BH1780_REG_CONTROL 0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define BH1780_REG_PARTID 0x0A
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #define BH1780_REG_MANFID 0x0B
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #define BH1780_REG_DLOW 0x0C
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define BH1780_REG_DHIGH 0x0D
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define BH1780_REVMASK GENMASK(3,0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #define BH1780_POWMASK GENMASK(1,0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #define BH1780_POFF (0x0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #define BH1780_PON (0x3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) /* power on settling time in ms */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #define BH1780_PON_DELAY 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) /* max time before value available in ms */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #define BH1780_INTERVAL 250
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) struct bh1780_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct i2c_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) static int bh1780_write(struct bh1780_data *bh1780, u8 reg, u8 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) int ret = i2c_smbus_write_byte_data(bh1780->client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) BH1780_CMD_BIT | reg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) dev_err(&bh1780->client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) "i2c_smbus_write_byte_data failed error "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) "%d, register %01x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) ret, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) static int bh1780_read(struct bh1780_data *bh1780, u8 reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) int ret = i2c_smbus_read_byte_data(bh1780->client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) BH1780_CMD_BIT | reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) dev_err(&bh1780->client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) "i2c_smbus_read_byte_data failed error "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) "%d, register %01x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) ret, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) static int bh1780_read_word(struct bh1780_data *bh1780, u8 reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) int ret = i2c_smbus_read_word_data(bh1780->client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) BH1780_CMD_BIT | reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) dev_err(&bh1780->client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) "i2c_smbus_read_word_data failed error "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) "%d, register %01x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) ret, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) static int bh1780_debugfs_reg_access(struct iio_dev *indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) unsigned int reg, unsigned int writeval,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) unsigned int *readval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) struct bh1780_data *bh1780 = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) if (!readval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) return bh1780_write(bh1780, (u8)reg, (u8)writeval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) ret = bh1780_read(bh1780, (u8)reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) *readval = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) static int bh1780_read_raw(struct iio_dev *indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) struct iio_chan_spec const *chan,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) int *val, int *val2, long mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) struct bh1780_data *bh1780 = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) int value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) switch (mask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) case IIO_CHAN_INFO_RAW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) switch (chan->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) case IIO_LIGHT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) pm_runtime_get_sync(&bh1780->client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) value = bh1780_read_word(bh1780, BH1780_REG_DLOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (value < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) pm_runtime_mark_last_busy(&bh1780->client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) pm_runtime_put_autosuspend(&bh1780->client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) *val = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) return IIO_VAL_INT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) case IIO_CHAN_INFO_INT_TIME:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) *val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) *val2 = BH1780_INTERVAL * 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return IIO_VAL_INT_PLUS_MICRO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) static const struct iio_info bh1780_info = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) .read_raw = bh1780_read_raw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) .debugfs_reg_access = bh1780_debugfs_reg_access,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) static const struct iio_chan_spec bh1780_channels[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) .type = IIO_LIGHT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) BIT(IIO_CHAN_INFO_INT_TIME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) static int bh1780_probe(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) const struct i2c_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) struct bh1780_data *bh1780;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) struct i2c_adapter *adapter = client->adapter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) struct iio_dev *indio_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*bh1780));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) if (!indio_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) bh1780 = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) bh1780->client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) i2c_set_clientdata(client, indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) /* Power up the device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) ret = bh1780_write(bh1780, BH1780_REG_CONTROL, BH1780_PON);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) msleep(BH1780_PON_DELAY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) pm_runtime_get_noresume(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) pm_runtime_set_active(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) pm_runtime_enable(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) ret = bh1780_read(bh1780, BH1780_REG_PARTID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) goto out_disable_pm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) dev_info(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) "Ambient Light Sensor, Rev : %lu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) (ret & BH1780_REVMASK));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) * As the device takes 250 ms to even come up with a fresh
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) * measurement after power-on, do not shut it down unnecessarily.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) * Set autosuspend to a five seconds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) pm_runtime_set_autosuspend_delay(&client->dev, 5000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) pm_runtime_use_autosuspend(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) pm_runtime_put(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) indio_dev->info = &bh1780_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) indio_dev->name = "bh1780";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) indio_dev->channels = bh1780_channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) indio_dev->num_channels = ARRAY_SIZE(bh1780_channels);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) indio_dev->modes = INDIO_DIRECT_MODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) ret = iio_device_register(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) goto out_disable_pm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) out_disable_pm:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) pm_runtime_put_noidle(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) pm_runtime_disable(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) static int bh1780_remove(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) struct iio_dev *indio_dev = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) struct bh1780_data *bh1780 = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) iio_device_unregister(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) pm_runtime_get_sync(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) pm_runtime_put_noidle(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) pm_runtime_disable(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) ret = bh1780_write(bh1780, BH1780_REG_CONTROL, BH1780_POFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) dev_err(&client->dev, "failed to power off\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) #ifdef CONFIG_PM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) static int bh1780_runtime_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) struct iio_dev *indio_dev = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) struct bh1780_data *bh1780 = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) ret = bh1780_write(bh1780, BH1780_REG_CONTROL, BH1780_POFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) dev_err(dev, "failed to runtime suspend\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) static int bh1780_runtime_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) struct iio_dev *indio_dev = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) struct bh1780_data *bh1780 = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) ret = bh1780_write(bh1780, BH1780_REG_CONTROL, BH1780_PON);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) dev_err(dev, "failed to runtime resume\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) /* Wait for power on, then for a value to be available */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) msleep(BH1780_PON_DELAY + BH1780_INTERVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) #endif /* CONFIG_PM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) static const struct dev_pm_ops bh1780_dev_pm_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) pm_runtime_force_resume)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) SET_RUNTIME_PM_OPS(bh1780_runtime_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) bh1780_runtime_resume, NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) static const struct i2c_device_id bh1780_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) { "bh1780", 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) { },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) MODULE_DEVICE_TABLE(i2c, bh1780_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) static const struct of_device_id of_bh1780_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) { .compatible = "rohm,bh1780gli", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) {},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) MODULE_DEVICE_TABLE(of, of_bh1780_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) static struct i2c_driver bh1780_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) .probe = bh1780_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) .remove = bh1780_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) .id_table = bh1780_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) .name = "bh1780",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) .pm = &bh1780_dev_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) .of_match_table = of_bh1780_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) module_i2c_driver(bh1780_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) MODULE_DESCRIPTION("ROHM BH1780GLI Ambient Light Sensor Driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");