2
2
lm78.c - Part of lm_sensors, Linux kernel modules for hardware
4
4
Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
5
Copyright (c) 2007 Jean Delvare <khali@linux-fr.org>
5
Copyright (c) 2007, 2011 Jean Delvare <khali@linux-fr.org>
7
7
This program is free software; you can redistribute it and/or modify
8
8
it under the terms of the GNU General Public License as published by
26
26
#include <linux/slab.h>
27
27
#include <linux/jiffies.h>
28
28
#include <linux/i2c.h>
29
#include <linux/platform_device.h>
30
#include <linux/ioport.h>
31
29
#include <linux/hwmon.h>
32
30
#include <linux/hwmon-vid.h>
33
31
#include <linux/hwmon-sysfs.h>
34
32
#include <linux/err.h>
35
33
#include <linux/mutex.h>
36
#include <linux/platform_device.h>
37
#include <linux/ioport.h>
36
38
#include <linux/io.h>
38
/* ISA device, if found */
39
static struct platform_device *pdev;
41
41
/* Addresses to scan */
42
42
static const unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
43
43
0x2e, 0x2f, I2C_CLIENT_END };
44
static unsigned short isa_address = 0x290;
46
44
enum chips { lm78, lm79 };
48
46
/* Many LM78 constants specified below */
146
static int lm78_i2c_detect(struct i2c_client *client,
147
struct i2c_board_info *info);
148
static int lm78_i2c_probe(struct i2c_client *client,
149
const struct i2c_device_id *id);
150
static int lm78_i2c_remove(struct i2c_client *client);
152
static int __devinit lm78_isa_probe(struct platform_device *pdev);
153
static int __devexit lm78_isa_remove(struct platform_device *pdev);
155
144
static int lm78_read_value(struct lm78_data *data, u8 reg);
156
145
static int lm78_write_value(struct lm78_data *data, u8 reg, u8 value);
157
146
static struct lm78_data *lm78_update_device(struct device *dev);
158
147
static void lm78_init_device(struct lm78_data *data);
161
static const struct i2c_device_id lm78_i2c_id[] = {
166
MODULE_DEVICE_TABLE(i2c, lm78_i2c_id);
168
static struct i2c_driver lm78_driver = {
169
.class = I2C_CLASS_HWMON,
173
.probe = lm78_i2c_probe,
174
.remove = lm78_i2c_remove,
175
.id_table = lm78_i2c_id,
176
.detect = lm78_i2c_detect,
177
.address_list = normal_i2c,
180
static struct platform_driver lm78_isa_driver = {
182
.owner = THIS_MODULE,
185
.probe = lm78_isa_probe,
186
.remove = __devexit_p(lm78_isa_remove),
191
151
static ssize_t show_in(struct device *dev, struct device_attribute *da,
514
474
.attrs = lm78_attributes,
482
/* ISA device, if found */
483
static struct platform_device *pdev;
485
static unsigned short isa_address = 0x290;
517
487
/* I2C devices get this name attribute automatically, but for ISA devices
518
488
we must create it by ourselves. */
519
489
static ssize_t show_name(struct device *dev, struct device_attribute
526
496
static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
498
static struct lm78_data *lm78_data_if_isa(void)
500
return pdev ? platform_get_drvdata(pdev) : NULL;
528
503
/* Returns 1 if the I2C chip appears to be an alias of the ISA chip */
529
504
static int lm78_alias_detect(struct i2c_client *client, u8 chipid)
536
#else /* !CONFIG_ISA */
538
static int lm78_alias_detect(struct i2c_client *client, u8 chipid)
543
static struct lm78_data *lm78_data_if_isa(void)
547
#endif /* CONFIG_ISA */
562
549
static int lm78_i2c_detect(struct i2c_client *client,
563
550
struct i2c_board_info *info)
566
struct lm78_data *isa = pdev ? platform_get_drvdata(pdev) : NULL;
553
struct lm78_data *isa = lm78_data_if_isa();
567
554
const char *client_name;
568
555
struct i2c_adapter *adapter = client->adapter;
569
556
int address = client->addr;
666
static int __devinit lm78_isa_probe(struct platform_device *pdev)
669
struct lm78_data *data;
670
struct resource *res;
672
/* Reserve the ISA region */
673
res = platform_get_resource(pdev, IORESOURCE_IO, 0);
674
if (!request_region(res->start + LM78_ADDR_REG_OFFSET, 2, "lm78")) {
679
if (!(data = kzalloc(sizeof(struct lm78_data), GFP_KERNEL))) {
681
goto exit_release_region;
683
mutex_init(&data->lock);
684
data->isa_addr = res->start;
685
platform_set_drvdata(pdev, data);
687
if (lm78_read_value(data, LM78_REG_CHIPID) & 0x80) {
695
/* Initialize the LM78 chip */
696
lm78_init_device(data);
698
/* Register sysfs hooks */
699
if ((err = sysfs_create_group(&pdev->dev.kobj, &lm78_group))
700
|| (err = device_create_file(&pdev->dev, &dev_attr_name)))
701
goto exit_remove_files;
703
data->hwmon_dev = hwmon_device_register(&pdev->dev);
704
if (IS_ERR(data->hwmon_dev)) {
705
err = PTR_ERR(data->hwmon_dev);
706
goto exit_remove_files;
712
sysfs_remove_group(&pdev->dev.kobj, &lm78_group);
713
device_remove_file(&pdev->dev, &dev_attr_name);
716
release_region(res->start + LM78_ADDR_REG_OFFSET, 2);
721
static int __devexit lm78_isa_remove(struct platform_device *pdev)
723
struct lm78_data *data = platform_get_drvdata(pdev);
724
struct resource *res;
726
hwmon_device_unregister(data->hwmon_dev);
727
sysfs_remove_group(&pdev->dev.kobj, &lm78_group);
728
device_remove_file(&pdev->dev, &dev_attr_name);
731
res = platform_get_resource(pdev, IORESOURCE_IO, 0);
732
release_region(res->start + LM78_ADDR_REG_OFFSET, 2);
653
static const struct i2c_device_id lm78_i2c_id[] = {
658
MODULE_DEVICE_TABLE(i2c, lm78_i2c_id);
660
static struct i2c_driver lm78_driver = {
661
.class = I2C_CLASS_HWMON,
665
.probe = lm78_i2c_probe,
666
.remove = lm78_i2c_remove,
667
.id_table = lm78_i2c_id,
668
.detect = lm78_i2c_detect,
669
.address_list = normal_i2c,
737
672
/* The SMBus locks itself, but ISA access must be locked explicitly!
738
673
We don't want to lock the whole ISA bus, so we lock each client
792
static int __devinit lm78_isa_probe(struct platform_device *pdev)
795
struct lm78_data *data;
796
struct resource *res;
798
/* Reserve the ISA region */
799
res = platform_get_resource(pdev, IORESOURCE_IO, 0);
800
if (!request_region(res->start + LM78_ADDR_REG_OFFSET, 2, "lm78")) {
805
data = kzalloc(sizeof(struct lm78_data), GFP_KERNEL);
808
goto exit_release_region;
810
mutex_init(&data->lock);
811
data->isa_addr = res->start;
812
platform_set_drvdata(pdev, data);
814
if (lm78_read_value(data, LM78_REG_CHIPID) & 0x80) {
822
/* Initialize the LM78 chip */
823
lm78_init_device(data);
825
/* Register sysfs hooks */
826
if ((err = sysfs_create_group(&pdev->dev.kobj, &lm78_group))
827
|| (err = device_create_file(&pdev->dev, &dev_attr_name)))
828
goto exit_remove_files;
830
data->hwmon_dev = hwmon_device_register(&pdev->dev);
831
if (IS_ERR(data->hwmon_dev)) {
832
err = PTR_ERR(data->hwmon_dev);
833
goto exit_remove_files;
839
sysfs_remove_group(&pdev->dev.kobj, &lm78_group);
840
device_remove_file(&pdev->dev, &dev_attr_name);
843
release_region(res->start + LM78_ADDR_REG_OFFSET, 2);
848
static int __devexit lm78_isa_remove(struct platform_device *pdev)
850
struct lm78_data *data = platform_get_drvdata(pdev);
851
struct resource *res;
853
hwmon_device_unregister(data->hwmon_dev);
854
sysfs_remove_group(&pdev->dev.kobj, &lm78_group);
855
device_remove_file(&pdev->dev, &dev_attr_name);
858
res = platform_get_resource(pdev, IORESOURCE_IO, 0);
859
release_region(res->start + LM78_ADDR_REG_OFFSET, 2);
864
static struct platform_driver lm78_isa_driver = {
866
.owner = THIS_MODULE,
869
.probe = lm78_isa_probe,
870
.remove = __devexit_p(lm78_isa_remove),
852
873
/* return 1 if a supported chip is found, 0 otherwise */
853
874
static int __init lm78_isa_found(unsigned short address)
972
static int __init sm_lm78_init(void)
993
static int __init lm78_isa_register(void)
976
/* We register the ISA device first, so that we can skip the
977
* registration of an I2C interface to the same device. */
978
997
if (lm78_isa_found(isa_address)) {
979
998
res = platform_driver_register(&lm78_isa_driver);
986
1005
goto exit_unreg_isa_driver;
989
res = i2c_add_driver(&lm78_driver);
991
goto exit_unreg_isa_device;
995
exit_unreg_isa_device:
996
platform_device_unregister(pdev);
997
1010
exit_unreg_isa_driver:
998
1011
platform_driver_unregister(&lm78_isa_driver);
1003
static void __exit sm_lm78_exit(void)
1016
static void lm78_isa_unregister(void)
1006
1019
platform_device_unregister(pdev);
1007
1020
platform_driver_unregister(&lm78_isa_driver);
1023
#else /* !CONFIG_ISA */
1025
static int __init lm78_isa_register(void)
1030
static void lm78_isa_unregister(void)
1033
#endif /* CONFIG_ISA */
1035
static int __init sm_lm78_init(void)
1039
/* We register the ISA device first, so that we can skip the
1040
* registration of an I2C interface to the same device. */
1041
res = lm78_isa_register();
1045
res = i2c_add_driver(&lm78_driver);
1047
goto exit_unreg_isa_device;
1051
exit_unreg_isa_device:
1052
lm78_isa_unregister();
1057
static void __exit sm_lm78_exit(void)
1059
lm78_isa_unregister();
1009
1060
i2c_del_driver(&lm78_driver);
1014
MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
1063
MODULE_AUTHOR("Frodo Looijaard, Jean Delvare <khali@linux-fr.org>");
1015
1064
MODULE_DESCRIPTION("LM78/LM79 driver");
1016
1065
MODULE_LICENSE("GPL");