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-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3)  * wm831x-otp.c  --  OTP for Wolfson WM831x PMICs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5)  * Copyright 2009 Wolfson Microelectronics PLC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7)  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9) 
^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/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/bcd.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/mfd/core.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/mfd/wm831x/core.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/mfd/wm831x/otp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) /* In bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define WM831X_UNIQUE_ID_LEN 16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) /* Read the unique ID from the chip into id */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) static int wm831x_unique_id_read(struct wm831x *wm831x, char *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) 	int i, val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) 	for (i = 0; i < WM831X_UNIQUE_ID_LEN / 2; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) 		val = wm831x_reg_read(wm831x, WM831X_UNIQUE_ID_1 + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) 		if (val < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) 			return val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) 		id[i * 2]       = (val >> 8) & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) 		id[(i * 2) + 1] = val & 0xff;
^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) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) static ssize_t wm831x_unique_id_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) 				     struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) 	struct wm831x *wm831x = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) 	int rval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) 	char id[WM831X_UNIQUE_ID_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) 	rval = wm831x_unique_id_read(wm831x, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) 	if (rval < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) 	return sprintf(buf, "%*phN\n", WM831X_UNIQUE_ID_LEN, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) static DEVICE_ATTR(unique_id, 0444, wm831x_unique_id_show, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) int wm831x_otp_init(struct wm831x *wm831x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) 	char uuid[WM831X_UNIQUE_ID_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) 	ret = device_create_file(wm831x->dev, &dev_attr_unique_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) 	if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) 		dev_err(wm831x->dev, "Unique ID attribute not created: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) 			ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) 	ret = wm831x_unique_id_read(wm831x, uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) 	if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) 		add_device_randomness(uuid, sizeof(uuid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) 		dev_err(wm831x->dev, "Failed to read UUID: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) void wm831x_otp_exit(struct wm831x *wm831x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) 	device_remove_file(wm831x->dev, &dev_attr_unique_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)