Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * pulsedlight-lidar-lite-v2.c - Support for PulsedLight LIDAR sensor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2015, 2017-2018
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Author: Matt Ranostay <matt.ranostay@konsulko.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * TODO: interrupt mode, and signal strength reporting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/i2c.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/iio/buffer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/iio/trigger.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/iio/triggered_buffer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/iio/trigger_consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #define LIDAR_REG_CONTROL		0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #define LIDAR_REG_CONTROL_ACQUIRE	BIT(2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #define LIDAR_REG_STATUS		0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #define LIDAR_REG_STATUS_INVALID	BIT(3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #define LIDAR_REG_STATUS_READY		BIT(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define LIDAR_REG_DATA_HBYTE		0x0f
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #define LIDAR_REG_DATA_LBYTE		0x10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #define LIDAR_REG_DATA_WORD_READ	BIT(7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define LIDAR_REG_PWR_CONTROL	0x65
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define LIDAR_DRV_NAME "lidar"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) struct lidar_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	struct iio_dev *indio_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	struct i2c_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	int (*xfer)(struct lidar_data *data, u8 reg, u8 *val, int len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	int i2c_enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	/* Ensure timestamp is naturally aligned */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		u16 chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		s64 timestamp __aligned(8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	} scan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) static const struct iio_chan_spec lidar_channels[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		.type = IIO_DISTANCE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		.info_mask_separate =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		.scan_index = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		.scan_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 			.sign = 'u',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 			.realbits = 16,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 			.storagebits = 16,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	IIO_CHAN_SOFT_TIMESTAMP(1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) static int lidar_i2c_xfer(struct lidar_data *data, u8 reg, u8 *val, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	struct i2c_msg msg[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	msg[0].addr = client->addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	msg[0].flags = client->flags | I2C_M_STOP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	msg[0].len = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	msg[0].buf  = (char *) &reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	msg[1].addr = client->addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	msg[1].flags = client->flags | I2C_M_RD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	msg[1].len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	msg[1].buf = (char *) val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	ret = i2c_transfer(client->adapter, msg, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	return (ret == 2) ? 0 : -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) static int lidar_smbus_xfer(struct lidar_data *data, u8 reg, u8 *val, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	 * Device needs a STOP condition between address write, and data read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	 * so in turn i2c_smbus_read_byte_data cannot be used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	while (len--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		ret = i2c_smbus_write_byte(client, reg++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 			dev_err(&client->dev, "cannot write addr value");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		ret = i2c_smbus_read_byte(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 			dev_err(&client->dev, "cannot read data value");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		*(val++) = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static int lidar_read_byte(struct lidar_data *data, u8 reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	u8 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	ret = data->xfer(data, reg, &val, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	return val;
^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 inline int lidar_write_control(struct lidar_data *data, int val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	return i2c_smbus_write_byte_data(data->client, LIDAR_REG_CONTROL, val);
^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 inline int lidar_write_power(struct lidar_data *data, int val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	return i2c_smbus_write_byte_data(data->client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 					 LIDAR_REG_PWR_CONTROL, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) static int lidar_read_measurement(struct lidar_data *data, u16 *reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	__be16 value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	int ret = data->xfer(data, LIDAR_REG_DATA_HBYTE |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 			(data->i2c_enabled ? LIDAR_REG_DATA_WORD_READ : 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 			(u8 *) &value, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		*reg = be16_to_cpu(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) static int lidar_get_measurement(struct lidar_data *data, u16 *reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	int tries = 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	pm_runtime_get_sync(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	/* start sample */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	ret = lidar_write_control(data, LIDAR_REG_CONTROL_ACQUIRE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		dev_err(&client->dev, "cannot send start measurement command");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		pm_runtime_put_noidle(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	while (tries--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		usleep_range(1000, 2000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		ret = lidar_read_byte(data, LIDAR_REG_STATUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		/* return -EINVAL since laser is likely pointed out of range */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		if (ret & LIDAR_REG_STATUS_INVALID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 			*reg = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		/* sample ready to read */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		if (!(ret & LIDAR_REG_STATUS_READY)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 			ret = lidar_read_measurement(data, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	pm_runtime_mark_last_busy(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	pm_runtime_put_autosuspend(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) static int lidar_read_raw(struct iio_dev *indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 			  struct iio_chan_spec const *chan,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 			  int *val, int *val2, long mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	struct lidar_data *data = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	int ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	switch (mask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	case IIO_CHAN_INFO_RAW: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		u16 reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		if (iio_device_claim_direct_mode(indio_dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 			return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		ret = lidar_get_measurement(data, &reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 			*val = reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 			ret = IIO_VAL_INT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		iio_device_release_direct_mode(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	case IIO_CHAN_INFO_SCALE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		*val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		*val2 = 10000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		ret = IIO_VAL_INT_PLUS_MICRO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) static irqreturn_t lidar_trigger_handler(int irq, void *private)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	struct iio_poll_func *pf = private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	struct iio_dev *indio_dev = pf->indio_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	struct lidar_data *data = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	ret = lidar_get_measurement(data, &data->scan.chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 						   iio_get_time_ns(indio_dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	} else if (ret != -EINVAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		dev_err(&data->client->dev, "cannot read LIDAR measurement");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	iio_trigger_notify_done(indio_dev->trig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) static const struct iio_info lidar_info = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	.read_raw = lidar_read_raw,
^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) static int lidar_probe(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		       const struct i2c_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	struct lidar_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	struct iio_dev *indio_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	if (!indio_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	data = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		data->xfer = lidar_i2c_xfer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		data->i2c_enabled = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	} else if (i2c_check_functionality(client->adapter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 				I2C_FUNC_SMBUS_WORD_DATA | I2C_FUNC_SMBUS_BYTE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		data->xfer = lidar_smbus_xfer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	indio_dev->info = &lidar_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	indio_dev->name = LIDAR_DRV_NAME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	indio_dev->channels = lidar_channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	indio_dev->num_channels = ARRAY_SIZE(lidar_channels);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	indio_dev->modes = INDIO_DIRECT_MODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	i2c_set_clientdata(client, indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	data->client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	data->indio_dev = indio_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	ret = iio_triggered_buffer_setup(indio_dev, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 					 lidar_trigger_handler, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	ret = iio_device_register(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		goto error_unreg_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	pm_runtime_set_autosuspend_delay(&client->dev, 1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	pm_runtime_use_autosuspend(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	ret = pm_runtime_set_active(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		goto error_unreg_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	pm_runtime_enable(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	pm_runtime_idle(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) error_unreg_buffer:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	iio_triggered_buffer_cleanup(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) static int lidar_remove(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	iio_device_unregister(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	iio_triggered_buffer_cleanup(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	pm_runtime_disable(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	pm_runtime_set_suspended(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) static const struct i2c_device_id lidar_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	{"lidar-lite-v2", 0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	{"lidar-lite-v3", 0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	{ },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) MODULE_DEVICE_TABLE(i2c, lidar_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) static const struct of_device_id lidar_dt_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	{ .compatible = "pulsedlight,lidar-lite-v2" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	{ .compatible = "grmn,lidar-lite-v3" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) MODULE_DEVICE_TABLE(of, lidar_dt_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) #ifdef CONFIG_PM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) static int lidar_pm_runtime_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	struct lidar_data *data = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	return lidar_write_power(data, 0x0f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) static int lidar_pm_runtime_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	struct lidar_data *data = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	int ret = lidar_write_power(data, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	/* regulator and FPGA needs settling time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	usleep_range(15000, 20000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) static const struct dev_pm_ops lidar_pm_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	SET_RUNTIME_PM_OPS(lidar_pm_runtime_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 			   lidar_pm_runtime_resume, NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) static struct i2c_driver lidar_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 		.name	= LIDAR_DRV_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 		.of_match_table	= lidar_dt_ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		.pm	= &lidar_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	.probe		= lidar_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	.remove		= lidar_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	.id_table	= lidar_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) module_i2c_driver(lidar_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) MODULE_DESCRIPTION("PulsedLight LIDAR sensor");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) MODULE_LICENSE("GPL");