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)  * Windfarm PowerMac thermal control.  SMU "satellite" controller sensors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2005 Paul Mackerras, IBM Corp. <paulus@samba.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/slab.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/wait.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <asm/prom.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <asm/smu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <asm/pmac_low_i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include "windfarm.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #define VERSION "1.0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) /* If the cache is older than 800ms we'll refetch it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #define MAX_AGE		msecs_to_jiffies(800)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) struct wf_sat {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	struct kref		ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	int			nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	struct mutex		mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	unsigned long		last_read; /* jiffies when cache last updated */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	u8			cache[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	struct list_head	sensors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	struct i2c_client	*i2c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	struct device_node	*node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) static struct wf_sat *sats[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) struct wf_sat_sensor {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	struct list_head	link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	int			index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	int			index2;		/* used for power sensors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	int			shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	struct wf_sat		*sat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	struct wf_sensor 	sens;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) #define wf_to_sat(c)	container_of(c, struct wf_sat_sensor, sens)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) struct smu_sdbp_header *smu_sat_get_sdb_partition(unsigned int sat_id, int id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 						  unsigned int *size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	struct wf_sat *sat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	unsigned int i, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	u8 *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	u8 data[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	/* TODO: Add the resulting partition to the device-tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	if (sat_id > 1 || (sat = sats[sat_id]) == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	err = i2c_smbus_write_word_data(sat->i2c, 8, id << 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		printk(KERN_ERR "smu_sat_get_sdb_part wr error %d\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	err = i2c_smbus_read_word_data(sat->i2c, 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		printk(KERN_ERR "smu_sat_get_sdb_part rd len error\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	len = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	if (len == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		printk(KERN_ERR "smu_sat_get_sdb_part no partition %x\n", id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		return NULL;
^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) 	len = le16_to_cpu(len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	len = (len + 3) & ~3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	buf = kmalloc(len, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	if (buf == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	for (i = 0; i < len; i += 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		err = i2c_smbus_read_i2c_block_data(sat->i2c, 0xa, 4, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 			printk(KERN_ERR "smu_sat_get_sdb_part rd err %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 			       err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		buf[i] = data[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		buf[i+1] = data[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		buf[i+2] = data[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		buf[i+3] = data[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	printk(KERN_DEBUG "sat %d partition %x:", sat_id, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	print_hex_dump(KERN_DEBUG, "  ", DUMP_PREFIX_OFFSET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		       16, 1, buf, len, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	if (size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		*size = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	return (struct smu_sdbp_header *) buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)  fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) EXPORT_SYMBOL_GPL(smu_sat_get_sdb_partition);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) /* refresh the cache */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) static int wf_sat_read_cache(struct wf_sat *sat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	err = i2c_smbus_read_i2c_block_data(sat->i2c, 0x3f, 16, sat->cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	sat->last_read = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) #ifdef LOTSA_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		printk(KERN_DEBUG "wf_sat_get: data is");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		print_hex_dump(KERN_DEBUG, "  ", DUMP_PREFIX_OFFSET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 			       16, 1, sat->cache, 16, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) static int wf_sat_sensor_get(struct wf_sensor *sr, s32 *value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	struct wf_sat_sensor *sens = wf_to_sat(sr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	struct wf_sat *sat = sens->sat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	int i, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	s32 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	if (sat->i2c == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	mutex_lock(&sat->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	if (time_after(jiffies, (sat->last_read + MAX_AGE))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		err = wf_sat_read_cache(sat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	i = sens->index * 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	val = ((sat->cache[i] << 8) + sat->cache[i+1]) << sens->shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	if (sens->index2 >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		i = sens->index2 * 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		/* 4.12 * 8.8 -> 12.20; shift right 4 to get 16.16 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		val = (val * ((sat->cache[i] << 8) + sat->cache[i+1])) >> 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	*value = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)  fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	mutex_unlock(&sat->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) static void wf_sat_release(struct kref *ref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	struct wf_sat *sat = container_of(ref, struct wf_sat, ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	if (sat->nr >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		sats[sat->nr] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	kfree(sat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) static void wf_sat_sensor_release(struct wf_sensor *sr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	struct wf_sat_sensor *sens = wf_to_sat(sr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	struct wf_sat *sat = sens->sat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	kfree(sens);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	kref_put(&sat->ref, wf_sat_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) static const struct wf_sensor_ops wf_sat_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	.get_value	= wf_sat_sensor_get,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	.release	= wf_sat_sensor_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	.owner		= THIS_MODULE,
^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) static int wf_sat_probe(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 			const struct i2c_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	struct device_node *dev = client->dev.of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	struct wf_sat *sat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	struct wf_sat_sensor *sens;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	const u32 *reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	const char *loc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	u8 chip, core;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	struct device_node *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	int shift, cpu, index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	int vsens[2], isens[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	sat = kzalloc(sizeof(struct wf_sat), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	if (sat == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	sat->nr = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	sat->node = of_node_get(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	kref_init(&sat->ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	mutex_init(&sat->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	sat->i2c = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	INIT_LIST_HEAD(&sat->sensors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	i2c_set_clientdata(client, sat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	vsens[0] = vsens[1] = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	isens[0] = isens[1] = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	for_each_child_of_node(dev, child) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		reg = of_get_property(child, "reg", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		loc = of_get_property(child, "location", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		if (reg == NULL || loc == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		/* the cooked sensors are between 0x30 and 0x37 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		if (*reg < 0x30 || *reg > 0x37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		index = *reg - 0x30;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		/* expect location to be CPU [AB][01] ... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		if (strncmp(loc, "CPU ", 4) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		chip = loc[4] - 'A';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		core = loc[5] - '0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		if (chip > 1 || core > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 			printk(KERN_ERR "wf_sat_create: don't understand "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 			       "location %s for %pOF\n", loc, child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		cpu = 2 * chip + core;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		if (sat->nr < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 			sat->nr = chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		else if (sat->nr != chip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 			printk(KERN_ERR "wf_sat_create: can't cope with "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 			       "multiple CPU chips on one SAT (%s)\n", loc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		if (of_node_is_type(child, "voltage-sensor")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 			name = "cpu-voltage";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 			shift = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 			vsens[core] = index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		} else if (of_node_is_type(child, "current-sensor")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 			name = "cpu-current";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 			shift = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 			isens[core] = index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		} else if (of_node_is_type(child, "temp-sensor")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 			name = "cpu-temp";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 			shift = 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 			continue;	/* hmmm shouldn't happen */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		/* the +16 is enough for "cpu-voltage-n" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		sens = kzalloc(sizeof(struct wf_sat_sensor) + 16, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		if (sens == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 			printk(KERN_ERR "wf_sat_create: couldn't create "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 			       "%s sensor %d (no memory)\n", name, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		sens->index = index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		sens->index2 = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		sens->shift = shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		sens->sat = sat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		sens->sens.ops = &wf_sat_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		sens->sens.name = (char *) (sens + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		snprintf((char *)sens->sens.name, 16, "%s-%d", name, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 		if (wf_register_sensor(&sens->sens))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 			kfree(sens);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 			list_add(&sens->link, &sat->sensors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 			kref_get(&sat->ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	/* make the power sensors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	for (core = 0; core < 2; ++core) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		if (vsens[core] < 0 || isens[core] < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		cpu = 2 * sat->nr + core;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		sens = kzalloc(sizeof(struct wf_sat_sensor) + 16, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		if (sens == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 			printk(KERN_ERR "wf_sat_create: couldn't create power "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 			       "sensor %d (no memory)\n", cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		sens->index = vsens[core];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		sens->index2 = isens[core];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		sens->shift = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		sens->sat = sat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		sens->sens.ops = &wf_sat_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		sens->sens.name = (char *) (sens + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		snprintf((char *)sens->sens.name, 16, "cpu-power-%d", cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		if (wf_register_sensor(&sens->sens))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 			kfree(sens);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 			list_add(&sens->link, &sat->sensors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 			kref_get(&sat->ref);
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	if (sat->nr >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		sats[sat->nr] = sat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) static int wf_sat_remove(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	struct wf_sat *sat = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	struct wf_sat_sensor *sens;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	/* release sensors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	while(!list_empty(&sat->sensors)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		sens = list_first_entry(&sat->sensors,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 					struct wf_sat_sensor, link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 		list_del(&sens->link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		wf_unregister_sensor(&sens->sens);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	sat->i2c = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	kref_put(&sat->ref, wf_sat_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	return 0;
^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) static const struct i2c_device_id wf_sat_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	{ "MAC,smu-sat", 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) MODULE_DEVICE_TABLE(i2c, wf_sat_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) static const struct of_device_id wf_sat_of_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	{ .compatible = "smu-sat", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) MODULE_DEVICE_TABLE(of, wf_sat_of_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) static struct i2c_driver wf_sat_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 		.name		= "wf_smu_sat",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 		.of_match_table = wf_sat_of_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	.probe		= wf_sat_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	.remove		= wf_sat_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	.id_table	= wf_sat_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) module_i2c_driver(wf_sat_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) MODULE_AUTHOR("Paul Mackerras <paulus@samba.org>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) MODULE_DESCRIPTION("SMU satellite sensors for PowerMac thermal control");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) MODULE_LICENSE("GPL");