blob: f4902f4cf8cbb7de79959323022da95054aeda63 [file] [log] [blame]
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +11001/*
2 * Windfarm PowerMac thermal control. MAX6690 sensor.
3 *
4 * Copyright (C) 2005 Paul Mackerras, IBM Corp. <paulus@samba.org>
5 *
6 * Use and redistribute under the terms of the GNU GPL v2.
7 */
8#include <linux/types.h>
9#include <linux/errno.h>
10#include <linux/kernel.h>
11#include <linux/init.h>
12#include <linux/slab.h>
13#include <linux/i2c.h>
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +110014#include <asm/prom.h>
15#include <asm/pmac_low_i2c.h>
16
17#include "windfarm.h"
18
Benjamin Herrenschmidt22b6d2e2012-04-18 22:16:45 +000019#define VERSION "1.0"
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +110020
21/* This currently only exports the external temperature sensor,
22 since that's all the control loops need. */
23
24/* Some MAX6690 register numbers */
25#define MAX6690_INTERNAL_TEMP 0
26#define MAX6690_EXTERNAL_TEMP 1
27
28struct wf_6690_sensor {
Jean Delvare351ca3e2009-06-15 18:01:51 +020029 struct i2c_client *i2c;
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +110030 struct wf_sensor sens;
31};
32
33#define wf_to_6690(x) container_of((x), struct wf_6690_sensor, sens)
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +110034
35static int wf_max6690_get(struct wf_sensor *sr, s32 *value)
36{
37 struct wf_6690_sensor *max = wf_to_6690(sr);
38 s32 data;
39
Jean Delvare351ca3e2009-06-15 18:01:51 +020040 if (max->i2c == NULL)
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +110041 return -ENODEV;
42
43 /* chip gets initialized by firmware */
Jean Delvare351ca3e2009-06-15 18:01:51 +020044 data = i2c_smbus_read_byte_data(max->i2c, MAX6690_EXTERNAL_TEMP);
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +110045 if (data < 0)
46 return data;
47 *value = data << 16;
48 return 0;
49}
50
51static void wf_max6690_release(struct wf_sensor *sr)
52{
53 struct wf_6690_sensor *max = wf_to_6690(sr);
54
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +110055 kfree(max);
56}
57
58static struct wf_sensor_ops wf_max6690_ops = {
59 .get_value = wf_max6690_get,
60 .release = wf_max6690_release,
61 .owner = THIS_MODULE,
62};
63
Jean Delvare351ca3e2009-06-15 18:01:51 +020064static int wf_max6690_probe(struct i2c_client *client,
65 const struct i2c_device_id *id)
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +110066{
Benjamin Herrenschmidt22b6d2e2012-04-18 22:16:45 +000067 const char *name, *loc;
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +110068 struct wf_6690_sensor *max;
Jean Delvare351ca3e2009-06-15 18:01:51 +020069 int rc;
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +110070
Benjamin Herrenschmidt22b6d2e2012-04-18 22:16:45 +000071 loc = of_get_property(client->dev.of_node, "hwsensor-location", NULL);
72 if (!loc) {
73 dev_warn(&client->dev, "Missing hwsensor-location property!\n");
74 return -ENXIO;
75 }
76
77 if (!strcmp(loc, "BACKSIDE"))
78 name = "backside-temp";
79 else if (!strcmp(loc, "NB Ambient"))
80 name = "north-bridge-temp";
81 else if (!strcmp(loc, "GPU Ambient"))
82 name = "gpu-temp";
83 else
84 return -ENXIO;
85
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +110086 max = kzalloc(sizeof(struct wf_6690_sensor), GFP_KERNEL);
87 if (max == NULL) {
Jean Delvare351ca3e2009-06-15 18:01:51 +020088 printk(KERN_ERR "windfarm: Couldn't create MAX6690 sensor: "
89 "no memory\n");
90 return -ENOMEM;
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +110091 }
92
Jean Delvare351ca3e2009-06-15 18:01:51 +020093 max->i2c = client;
Benjamin Herrenschmidt22b6d2e2012-04-18 22:16:45 +000094 max->sens.name = (char *)name; /* XXX fix constness in structure */
Jean Delvare351ca3e2009-06-15 18:01:51 +020095 max->sens.ops = &wf_max6690_ops;
96 i2c_set_clientdata(client, max);
97
98 rc = wf_register_sensor(&max->sens);
Benjamin Herrenschmidt22b6d2e2012-04-18 22:16:45 +000099 if (rc)
Jean Delvare351ca3e2009-06-15 18:01:51 +0200100 kfree(max);
Jean Delvare351ca3e2009-06-15 18:01:51 +0200101 return rc;
102}
103
Jean Delvare351ca3e2009-06-15 18:01:51 +0200104static int wf_max6690_remove(struct i2c_client *client)
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100105{
Jean Delvare351ca3e2009-06-15 18:01:51 +0200106 struct wf_6690_sensor *max = i2c_get_clientdata(client);
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100107
Jean Delvare351ca3e2009-06-15 18:01:51 +0200108 max->i2c = NULL;
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100109 wf_unregister_sensor(&max->sens);
110
111 return 0;
112}
113
Jean Delvare351ca3e2009-06-15 18:01:51 +0200114static const struct i2c_device_id wf_max6690_id[] = {
Benjamin Herrenschmidt22b6d2e2012-04-18 22:16:45 +0000115 { "MAC,max6690", 0 },
Jean Delvare351ca3e2009-06-15 18:01:51 +0200116 { }
117};
Benjamin Herrenschmidt22b6d2e2012-04-18 22:16:45 +0000118MODULE_DEVICE_TABLE(i2c, wf_max6690_id);
Jean Delvare351ca3e2009-06-15 18:01:51 +0200119
120static struct i2c_driver wf_max6690_driver = {
121 .driver = {
122 .name = "wf_max6690",
123 },
Jean Delvare351ca3e2009-06-15 18:01:51 +0200124 .probe = wf_max6690_probe,
125 .remove = wf_max6690_remove,
126 .id_table = wf_max6690_id,
127};
128
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100129static int __init wf_max6690_sensor_init(void)
130{
Benjamin Herrenschmidtb55fafc2006-03-03 17:03:21 +1100131 /* Don't register on old machines that use therm_pm72 for now */
Grant Likely71a157e2010-02-01 21:34:14 -0700132 if (of_machine_is_compatible("PowerMac7,2") ||
133 of_machine_is_compatible("PowerMac7,3") ||
134 of_machine_is_compatible("RackMac3,1"))
Benjamin Herrenschmidtb55fafc2006-03-03 17:03:21 +1100135 return -ENODEV;
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100136 return i2c_add_driver(&wf_max6690_driver);
137}
138
139static void __exit wf_max6690_sensor_exit(void)
140{
141 i2c_del_driver(&wf_max6690_driver);
142}
143
144module_init(wf_max6690_sensor_init);
145module_exit(wf_max6690_sensor_exit);
146
147MODULE_AUTHOR("Paul Mackerras <paulus@samba.org>");
148MODULE_DESCRIPTION("MAX6690 sensor objects for PowerMac thermal control");
149MODULE_LICENSE("GPL");