1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Power Management and GPIO expander driver for MPC8349E-mITX-compatible MCU 4 * 5 * Copyright (c) 2008 MontaVista Software, Inc. 6 * 7 * Author: Anton Vorontsov <avorontsov@ru.mvista.com> 8 */ 9 10 #include <linux/kernel.h> 11 #include <linux/mod_devicetable.h> 12 #include <linux/module.h> 13 #include <linux/device.h> 14 #include <linux/mutex.h> 15 #include <linux/i2c.h> 16 #include <linux/gpio/driver.h> 17 #include <linux/slab.h> 18 #include <linux/kthread.h> 19 #include <linux/property.h> 20 #include <linux/reboot.h> 21 #include <asm/prom.h> 22 #include <asm/machdep.h> 23 24 /* 25 * I don't have specifications for the MCU firmware, I found this register 26 * and bits positions by the trial&error method. 27 */ 28 #define MCU_REG_CTRL 0x20 29 #define MCU_CTRL_POFF 0x40 30 #define MCU_CTRL_BTN 0x80 31 32 #define MCU_NUM_GPIO 2 33 34 struct mcu { 35 struct mutex lock; 36 struct i2c_client *client; 37 struct gpio_chip gc; 38 u8 reg_ctrl; 39 }; 40 41 static struct mcu *glob_mcu; 42 43 struct task_struct *shutdown_thread; 44 static int shutdown_thread_fn(void *data) 45 { 46 int ret; 47 struct mcu *mcu = glob_mcu; 48 49 while (!kthread_should_stop()) { 50 ret = i2c_smbus_read_byte_data(mcu->client, MCU_REG_CTRL); 51 if (ret < 0) 52 pr_err("MCU status reg read failed.\n"); 53 mcu->reg_ctrl = ret; 54 55 56 if (mcu->reg_ctrl & MCU_CTRL_BTN) { 57 i2c_smbus_write_byte_data(mcu->client, MCU_REG_CTRL, 58 mcu->reg_ctrl & ~MCU_CTRL_BTN); 59 60 ctrl_alt_del(); 61 } 62 63 set_current_state(TASK_INTERRUPTIBLE); 64 schedule_timeout(HZ); 65 } 66 67 return 0; 68 } 69 70 static ssize_t show_status(struct device *d, 71 struct device_attribute *attr, char *buf) 72 { 73 int ret; 74 struct mcu *mcu = glob_mcu; 75 76 ret = i2c_smbus_read_byte_data(mcu->client, MCU_REG_CTRL); 77 if (ret < 0) 78 return -ENODEV; 79 mcu->reg_ctrl = ret; 80 81 return sprintf(buf, "%02x\n", ret); 82 } 83 static DEVICE_ATTR(status, 0444, show_status, NULL); 84 85 static void mcu_power_off(void) 86 { 87 struct mcu *mcu = glob_mcu; 88 89 pr_info("Sending power-off request to the MCU...\n"); 90 mutex_lock(&mcu->lock); 91 i2c_smbus_write_byte_data(mcu->client, MCU_REG_CTRL, 92 mcu->reg_ctrl | MCU_CTRL_POFF); 93 mutex_unlock(&mcu->lock); 94 } 95 96 static void mcu_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val) 97 { 98 struct mcu *mcu = gpiochip_get_data(gc); 99 u8 bit = 1 << (4 + gpio); 100 101 mutex_lock(&mcu->lock); 102 if (val) 103 mcu->reg_ctrl &= ~bit; 104 else 105 mcu->reg_ctrl |= bit; 106 107 i2c_smbus_write_byte_data(mcu->client, MCU_REG_CTRL, mcu->reg_ctrl); 108 mutex_unlock(&mcu->lock); 109 } 110 111 static int mcu_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val) 112 { 113 mcu_gpio_set(gc, gpio, val); 114 return 0; 115 } 116 117 static int mcu_gpiochip_add(struct mcu *mcu) 118 { 119 struct device *dev = &mcu->client->dev; 120 struct gpio_chip *gc = &mcu->gc; 121 122 gc->owner = THIS_MODULE; 123 gc->label = kasprintf(GFP_KERNEL, "%pfw", dev_fwnode(dev)); 124 gc->can_sleep = 1; 125 gc->ngpio = MCU_NUM_GPIO; 126 gc->base = -1; 127 gc->set = mcu_gpio_set; 128 gc->direction_output = mcu_gpio_dir_out; 129 gc->parent = dev; 130 131 return gpiochip_add_data(gc, mcu); 132 } 133 134 static void mcu_gpiochip_remove(struct mcu *mcu) 135 { 136 kfree(mcu->gc.label); 137 gpiochip_remove(&mcu->gc); 138 } 139 140 static int mcu_probe(struct i2c_client *client) 141 { 142 struct mcu *mcu; 143 int ret; 144 145 mcu = kzalloc(sizeof(*mcu), GFP_KERNEL); 146 if (!mcu) 147 return -ENOMEM; 148 149 mutex_init(&mcu->lock); 150 mcu->client = client; 151 i2c_set_clientdata(client, mcu); 152 153 ret = i2c_smbus_read_byte_data(mcu->client, MCU_REG_CTRL); 154 if (ret < 0) 155 goto err; 156 mcu->reg_ctrl = ret; 157 158 ret = mcu_gpiochip_add(mcu); 159 if (ret) 160 goto err; 161 162 /* XXX: this is potentially racy, but there is no lock for pm_power_off */ 163 if (!pm_power_off) { 164 glob_mcu = mcu; 165 pm_power_off = mcu_power_off; 166 dev_info(&client->dev, "will provide power-off service\n"); 167 } 168 169 if (device_create_file(&client->dev, &dev_attr_status)) 170 dev_err(&client->dev, 171 "couldn't create device file for status\n"); 172 173 shutdown_thread = kthread_run(shutdown_thread_fn, NULL, 174 "mcu-i2c-shdn"); 175 176 return 0; 177 err: 178 kfree(mcu); 179 return ret; 180 } 181 182 static int mcu_remove(struct i2c_client *client) 183 { 184 struct mcu *mcu = i2c_get_clientdata(client); 185 186 kthread_stop(shutdown_thread); 187 188 device_remove_file(&client->dev, &dev_attr_status); 189 190 if (glob_mcu == mcu) { 191 pm_power_off = NULL; 192 glob_mcu = NULL; 193 } 194 195 mcu_gpiochip_remove(mcu); 196 kfree(mcu); 197 return 0; 198 } 199 200 static const struct i2c_device_id mcu_ids[] = { 201 { "mcu-mpc8349emitx", }, 202 {}, 203 }; 204 MODULE_DEVICE_TABLE(i2c, mcu_ids); 205 206 static const struct of_device_id mcu_of_match_table[] = { 207 { .compatible = "fsl,mcu-mpc8349emitx", }, 208 { }, 209 }; 210 211 static struct i2c_driver mcu_driver = { 212 .driver = { 213 .name = "mcu-mpc8349emitx", 214 .of_match_table = mcu_of_match_table, 215 }, 216 .probe_new = mcu_probe, 217 .remove = mcu_remove, 218 .id_table = mcu_ids, 219 }; 220 221 module_i2c_driver(mcu_driver); 222 223 MODULE_DESCRIPTION("Power Management and GPIO expander driver for " 224 "MPC8349E-mITX-compatible MCU"); 225 MODULE_AUTHOR("Anton Vorontsov <avorontsov@ru.mvista.com>"); 226 MODULE_LICENSE("GPL"); 227