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-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * max6875.c - driver for MAX6874/MAX6875
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2005 Ben Gardner <bgardner@wabtec.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Based on eeprom.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * The MAX6875 has a bank of registers and two banks of EEPROM.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * Address ranges are defined as follows:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  *  * 0x0000 - 0x0046 = configuration registers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  *  * 0x8000 - 0x8046 = configuration EEPROM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  *  * 0x8100 - 0x82FF = user EEPROM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  * This driver makes the user EEPROM available for read.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * The registers & config EEPROM should be accessed via i2c-dev.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * The MAX6875 ignores the lowest address bit, so each chip responds to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * two addresses - 0x50/0x51 and 0x52/0x53.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * Note that the MAX6875 uses i2c_smbus_write_byte_data() to set the read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * address, so this driver is destructive if loaded for the wrong EEPROM chip.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) /* The MAX6875 can only read/write 16 bytes at a time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #define SLICE_SIZE			16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #define SLICE_BITS			4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) /* USER EEPROM is at addresses 0x8100 - 0x82FF */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define USER_EEPROM_BASE		0x8100
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define USER_EEPROM_SIZE		0x0200
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #define USER_EEPROM_SLICES		32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) /* MAX6875 commands */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #define MAX6875_CMD_BLK_READ		0x84
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) /* Each client has this additional data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) struct max6875_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	struct i2c_client	*fake_client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	struct mutex		update_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	u32			valid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	u8			data[USER_EEPROM_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	unsigned long		last_updated[USER_EEPROM_SLICES];
^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 void max6875_update_slice(struct i2c_client *client, int slice)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	struct max6875_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	int i, j, addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	u8 *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	if (slice >= USER_EEPROM_SLICES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	buf = &data->data[slice << SLICE_BITS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	if (!(data->valid & (1 << slice)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	    time_after(jiffies, data->last_updated[slice])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		dev_dbg(&client->dev, "Starting update of slice %u\n", slice);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		data->valid &= ~(1 << slice);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		addr = USER_EEPROM_BASE + (slice << SLICE_BITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		/* select the eeprom address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		if (i2c_smbus_write_byte_data(client, addr >> 8, addr & 0xFF)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 			dev_err(&client->dev, "address set failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 			goto exit_up;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		if (i2c_check_functionality(client->adapter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 					    I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 			if (i2c_smbus_read_i2c_block_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 							  MAX6875_CMD_BLK_READ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 							  SLICE_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 							  buf) != SLICE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 				goto exit_up;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 			for (i = 0; i < SLICE_SIZE; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 				j = i2c_smbus_read_byte(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 				if (j < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 					goto exit_up;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 				buf[i] = j;
^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) 		data->last_updated[slice] = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		data->valid |= (1 << slice);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) exit_up:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) static ssize_t max6875_read(struct file *filp, struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 			    struct bin_attribute *bin_attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 			    char *buf, loff_t off, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	struct i2c_client *client = kobj_to_i2c_client(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	struct max6875_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	int slice, max_slice;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	/* refresh slices which contain requested bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	max_slice = (off + count - 1) >> SLICE_BITS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	for (slice = (off >> SLICE_BITS); slice <= max_slice; slice++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		max6875_update_slice(client, slice);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	memcpy(buf, &data->data[off], count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) static const struct bin_attribute user_eeprom_attr = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	.attr = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		.name = "eeprom",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		.mode = S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	.size = USER_EEPROM_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	.read = max6875_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) static int max6875_probe(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 			 const struct i2c_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	struct i2c_adapter *adapter = client->adapter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	struct max6875_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WRITE_BYTE_DATA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 				     | I2C_FUNC_SMBUS_READ_BYTE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	/* Only bind to even addresses */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	if (client->addr & 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	data = kzalloc(sizeof(struct max6875_data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	/* A fake client is created on the odd address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	data->fake_client = i2c_new_dummy_device(client->adapter, client->addr + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	if (IS_ERR(data->fake_client)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		err = PTR_ERR(data->fake_client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		goto exit_kfree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	/* Init real i2c_client */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	i2c_set_clientdata(client, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	mutex_init(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	err = sysfs_create_bin_file(&client->dev.kobj, &user_eeprom_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		goto exit_remove_fake;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) exit_remove_fake:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	i2c_unregister_device(data->fake_client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) exit_kfree:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	kfree(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) static int max6875_remove(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	struct max6875_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	i2c_unregister_device(data->fake_client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	sysfs_remove_bin_file(&client->dev.kobj, &user_eeprom_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	kfree(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) static const struct i2c_device_id max6875_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	{ "max6875", 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) MODULE_DEVICE_TABLE(i2c, max6875_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) static struct i2c_driver max6875_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		.name	= "max6875",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	.probe		= max6875_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	.remove		= max6875_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	.id_table	= max6875_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) module_i2c_driver(max6875_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) MODULE_AUTHOR("Ben Gardner <bgardner@wabtec.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) MODULE_DESCRIPTION("MAX6875 driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) MODULE_LICENSE("GPL");