Thomas Gleixner | ddceed9 | 2019-06-03 07:45:03 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 2 | /* |
| 3 | * Windfarm PowerMac thermal control. MAX6690 sensor. |
| 4 | * |
| 5 | * Copyright (C) 2005 Paul Mackerras, IBM Corp. <paulus@samba.org> |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 6 | */ |
| 7 | #include <linux/types.h> |
| 8 | #include <linux/errno.h> |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/init.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/i2c.h> |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 13 | #include <asm/prom.h> |
| 14 | #include <asm/pmac_low_i2c.h> |
| 15 | |
| 16 | #include "windfarm.h" |
| 17 | |
Benjamin Herrenschmidt | 22b6d2e | 2012-04-18 22:16:45 +0000 | [diff] [blame] | 18 | #define VERSION "1.0" |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 19 | |
| 20 | /* This currently only exports the external temperature sensor, |
| 21 | since that's all the control loops need. */ |
| 22 | |
| 23 | /* Some MAX6690 register numbers */ |
| 24 | #define MAX6690_INTERNAL_TEMP 0 |
| 25 | #define MAX6690_EXTERNAL_TEMP 1 |
| 26 | |
| 27 | struct wf_6690_sensor { |
Jean Delvare | 351ca3e | 2009-06-15 18:01:51 +0200 | [diff] [blame] | 28 | struct i2c_client *i2c; |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 29 | struct wf_sensor sens; |
| 30 | }; |
| 31 | |
| 32 | #define wf_to_6690(x) container_of((x), struct wf_6690_sensor, sens) |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 33 | |
| 34 | static int wf_max6690_get(struct wf_sensor *sr, s32 *value) |
| 35 | { |
| 36 | struct wf_6690_sensor *max = wf_to_6690(sr); |
| 37 | s32 data; |
| 38 | |
Jean Delvare | 351ca3e | 2009-06-15 18:01:51 +0200 | [diff] [blame] | 39 | if (max->i2c == NULL) |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 40 | return -ENODEV; |
| 41 | |
| 42 | /* chip gets initialized by firmware */ |
Jean Delvare | 351ca3e | 2009-06-15 18:01:51 +0200 | [diff] [blame] | 43 | data = i2c_smbus_read_byte_data(max->i2c, MAX6690_EXTERNAL_TEMP); |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 44 | if (data < 0) |
| 45 | return data; |
| 46 | *value = data << 16; |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | static void wf_max6690_release(struct wf_sensor *sr) |
| 51 | { |
| 52 | struct wf_6690_sensor *max = wf_to_6690(sr); |
| 53 | |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 54 | kfree(max); |
| 55 | } |
| 56 | |
Julia Lawall | de854e5 | 2017-08-02 23:01:45 +0200 | [diff] [blame] | 57 | static const struct wf_sensor_ops wf_max6690_ops = { |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 58 | .get_value = wf_max6690_get, |
| 59 | .release = wf_max6690_release, |
| 60 | .owner = THIS_MODULE, |
| 61 | }; |
| 62 | |
Jean Delvare | 351ca3e | 2009-06-15 18:01:51 +0200 | [diff] [blame] | 63 | static int wf_max6690_probe(struct i2c_client *client, |
| 64 | const struct i2c_device_id *id) |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 65 | { |
Benjamin Herrenschmidt | 22b6d2e | 2012-04-18 22:16:45 +0000 | [diff] [blame] | 66 | const char *name, *loc; |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 67 | struct wf_6690_sensor *max; |
Jean Delvare | 351ca3e | 2009-06-15 18:01:51 +0200 | [diff] [blame] | 68 | int rc; |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 69 | |
Benjamin Herrenschmidt | 22b6d2e | 2012-04-18 22:16:45 +0000 | [diff] [blame] | 70 | loc = of_get_property(client->dev.of_node, "hwsensor-location", NULL); |
| 71 | if (!loc) { |
| 72 | dev_warn(&client->dev, "Missing hwsensor-location property!\n"); |
| 73 | return -ENXIO; |
| 74 | } |
| 75 | |
Benjamin Herrenschmidt | d839ba2 | 2012-04-18 22:16:54 +0000 | [diff] [blame] | 76 | /* |
| 77 | * We only expose the external temperature register for |
| 78 | * now as this is all we need for our control loops |
| 79 | */ |
| 80 | if (!strcmp(loc, "BACKSIDE") || !strcmp(loc, "SYS CTRLR AMBIENT")) |
Benjamin Herrenschmidt | 22b6d2e | 2012-04-18 22:16:45 +0000 | [diff] [blame] | 81 | name = "backside-temp"; |
| 82 | else if (!strcmp(loc, "NB Ambient")) |
| 83 | name = "north-bridge-temp"; |
| 84 | else if (!strcmp(loc, "GPU Ambient")) |
| 85 | name = "gpu-temp"; |
| 86 | else |
| 87 | return -ENXIO; |
| 88 | |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 89 | max = kzalloc(sizeof(struct wf_6690_sensor), GFP_KERNEL); |
| 90 | if (max == NULL) { |
Jean Delvare | 351ca3e | 2009-06-15 18:01:51 +0200 | [diff] [blame] | 91 | printk(KERN_ERR "windfarm: Couldn't create MAX6690 sensor: " |
| 92 | "no memory\n"); |
| 93 | return -ENOMEM; |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 94 | } |
| 95 | |
Jean Delvare | 351ca3e | 2009-06-15 18:01:51 +0200 | [diff] [blame] | 96 | max->i2c = client; |
Geert Uytterhoeven | 8bb61fe | 2013-11-12 20:07:14 +0100 | [diff] [blame] | 97 | max->sens.name = name; |
Jean Delvare | 351ca3e | 2009-06-15 18:01:51 +0200 | [diff] [blame] | 98 | max->sens.ops = &wf_max6690_ops; |
| 99 | i2c_set_clientdata(client, max); |
| 100 | |
| 101 | rc = wf_register_sensor(&max->sens); |
Benjamin Herrenschmidt | 22b6d2e | 2012-04-18 22:16:45 +0000 | [diff] [blame] | 102 | if (rc) |
Jean Delvare | 351ca3e | 2009-06-15 18:01:51 +0200 | [diff] [blame] | 103 | kfree(max); |
Jean Delvare | 351ca3e | 2009-06-15 18:01:51 +0200 | [diff] [blame] | 104 | return rc; |
| 105 | } |
| 106 | |
Jean Delvare | 351ca3e | 2009-06-15 18:01:51 +0200 | [diff] [blame] | 107 | static int wf_max6690_remove(struct i2c_client *client) |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 108 | { |
Jean Delvare | 351ca3e | 2009-06-15 18:01:51 +0200 | [diff] [blame] | 109 | struct wf_6690_sensor *max = i2c_get_clientdata(client); |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 110 | |
Jean Delvare | 351ca3e | 2009-06-15 18:01:51 +0200 | [diff] [blame] | 111 | max->i2c = NULL; |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 112 | wf_unregister_sensor(&max->sens); |
| 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
Jean Delvare | 351ca3e | 2009-06-15 18:01:51 +0200 | [diff] [blame] | 117 | static const struct i2c_device_id wf_max6690_id[] = { |
Benjamin Herrenschmidt | 22b6d2e | 2012-04-18 22:16:45 +0000 | [diff] [blame] | 118 | { "MAC,max6690", 0 }, |
Jean Delvare | 351ca3e | 2009-06-15 18:01:51 +0200 | [diff] [blame] | 119 | { } |
| 120 | }; |
Benjamin Herrenschmidt | 22b6d2e | 2012-04-18 22:16:45 +0000 | [diff] [blame] | 121 | MODULE_DEVICE_TABLE(i2c, wf_max6690_id); |
Jean Delvare | 351ca3e | 2009-06-15 18:01:51 +0200 | [diff] [blame] | 122 | |
Wolfram Sang | bcf3588 | 2020-03-03 13:50:46 +0100 | [diff] [blame] | 123 | static const struct of_device_id wf_max6690_of_id[] = { |
| 124 | { .compatible = "max6690", }, |
| 125 | { } |
| 126 | }; |
| 127 | MODULE_DEVICE_TABLE(of, wf_max6690_of_id); |
| 128 | |
Jean Delvare | 351ca3e | 2009-06-15 18:01:51 +0200 | [diff] [blame] | 129 | static struct i2c_driver wf_max6690_driver = { |
| 130 | .driver = { |
| 131 | .name = "wf_max6690", |
Wolfram Sang | bcf3588 | 2020-03-03 13:50:46 +0100 | [diff] [blame] | 132 | .of_match_table = wf_max6690_of_id, |
Jean Delvare | 351ca3e | 2009-06-15 18:01:51 +0200 | [diff] [blame] | 133 | }, |
Jean Delvare | 351ca3e | 2009-06-15 18:01:51 +0200 | [diff] [blame] | 134 | .probe = wf_max6690_probe, |
| 135 | .remove = wf_max6690_remove, |
| 136 | .id_table = wf_max6690_id, |
| 137 | }; |
| 138 | |
Wei Yongjun | f7fb862 | 2012-10-08 03:00:04 +0000 | [diff] [blame] | 139 | module_i2c_driver(wf_max6690_driver); |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 140 | |
| 141 | MODULE_AUTHOR("Paul Mackerras <paulus@samba.org>"); |
| 142 | MODULE_DESCRIPTION("MAX6690 sensor objects for PowerMac thermal control"); |
| 143 | MODULE_LICENSE("GPL"); |